MinGW + Boost: undefined reference to `WSAStartup@8'

后端 未结 1 1830
北海茫月
北海茫月 2020-12-20 15:38

below is what I execute

D:\\Just4Fun\\software\\>make -f Makefile.hands stest
g++.exe -g -D_WIN32_WINNT=0x0601 -ID:\\Boost\\boost_1_51_0 -LD:\\Boost\

相关标签:
1条回答
  • 2020-12-20 16:28

    It's very common error, with a very simple solution: Place the libraries you link with after the source and object files.


    If an object (or source) file A depends on a library B, then A must come before B on the command line when linking. This is because of how the GNU (and possibly other) linkers solve symbol dependencies.

    If doing it the opposite (and wrong) way

    g++ -lB A.cpp
    

    then when the linker loads the library B, there are no dependencies on any of the functions in the library, so the linker just discards that library.

    So the right way is to put the library last

    g++ A.cpp -lB
    

    This is the reason many examples will always put libraries last on the command line.

    So for the command line of the question, it should instead be

    g++.exe -g -D_WIN32_WINNT=0x0601 -ID:\Boost\boost_1_51_0 \
        -LD:\Boost\boost_1_51_0\stage\lib -LD:\MinGW\lib \
        -o TestSerial.exe TestSerial.cpp \
        -lboost_system-mgw46-d-1_51 -lboost_filesystem-mgw46-d-1_51 -lboost_iostreams-mgw46-d-1_51 -lws2_32 -lwsock32
    

    This order is also important for inter-library dependencies. If library L1 depends on library L2, then L1 must be before L2 on the command line.

    0 讨论(0)
提交回复
热议问题