简体   繁体   English

MinGW + Boost:对“ WSAStartup @ 8”的未定义引用

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

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\\boost_1_51_0\\stage\\lib -LD:\\MinGW\\lib -lboost_system-mgw46-d-1_51 -lboost_filesystem-mgw46-d-1_51 -lboost_iostreams-mgw46-d-1_51 -lws2_32 -lwsock32 -o TestSerial.exe TestSerial.cpp

below is the result of that 下面是那个的结果

C:\\Users\\oleg\\AppData\\Local\\Temp\\ccXpC7RG.o: In function 'ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh': D:/Boost/boost_1_51_0/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to 'WSAStartup@8'C:\\Users\\oleg\\AppData\\Local\\Temp\\ccXpC7RG.o: In function 'ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE': D:/Boost/boost_1_51_0/boost/asio/detail/impl/winsock_init.ipp:48: undefined reference to 'WSACleanup@0' collect2: ld returned 1 exit status make: *** [stest] Error 1

Below is result of checking libraries regarding to mentioned symobls (I failed to format it in appropriate way) 以下是检查有关提到的符号的库的结果(我无法以适当的方式对其进行格式化)

`d:\\MinGW\\lib>nm -C libws2_32.a | `d:\\ MinGW \\ lib> nm -C libws2_32.a | grep WSAStartup grep WSAStartup
00000000 T WSAStartup@8 00000000吨WSAStartup @ 8
00000000 I imp _WSAStartup@8 00000000我imp _WSAStartup @ 8

d:\\MinGW\\lib>nm -C libws2_32.a | d:\\ MinGW \\ lib> nm -C libws2_32.a | grep WSACleanup grep WSACleanup
00000000 T WSACleanup@0 00000000吨WSACleanup @ 0
00000000 I imp _WSACleanup@0 00000000我imp _WSACleanup @ 0

d:\\MinGW\\lib>nm -C libwsock32.a | d:\\ MinGW \\ lib> nm -C libwsock32.a | grep WSACleanup grep WSACleanup
00000000 T WSACleanup@0 00000000吨WSACleanup @ 0
00000000 I imp _WSACleanup@0 00000000我imp _WSACleanup @ 0

d:\\MinGW\\lib>nm -C libwsock32.a | d:\\ MinGW \\ lib> nm -C libwsock32.a | grep WSAStartup grep WSAStartup
00000000 T WSAStartup@8 00000000吨WSAStartup @ 8
00000000 I imp _WSAStartup@8` 00000000我imp _WSAStartup @ 8`

As wee see symbols are existing in the libraries and libraries are on the place referred by the corresponding -L command line options. 如我们所见,库中存在符号,并且库位于相应的-L命令行选项所指的位置。
So, why "undefined refernce"???? 那么,为什么“未定义引用”呢????

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. 如果对象(或源)文件A依赖于库B,则在链接时,命令行上A必须在B之前。 This is because of how the GNU (and possibly other) linkers solve symbol dependencies. 这是因为GNU(以及可能的其他)链接器如何解决符号依赖性。

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. 然后,当链接器加载库B时,该库中的任何功能都没有依赖关系,因此链接器仅丢弃该库。

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. 如果库L1依赖于库L2,则在命令行上L1必须在L2之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在代码块上未定义对 wsastartup@8' 的引用,帮帮我 - undefined reference to wsastartup@8' on Code blocks, help me C++ 未定义对“WSAStartup@8”的引用,即使我链接了 ws2_32.lib - C++ undefined reference to 'WSAStartup@8' even though i linked ws2_32.lib 在MinGW中使用boost时,未定义对WinMain @ 16的引用 - undefined reference to WinMain@16 when using boost with MinGW 在mingw64上对带有boost.thread的InterlockedCompareExchange的未定义引用(但在mingw32上未引用) - Undefined reference to InterlockedCompareExchange with boost.thread on mingw64 (but not on mingw32) Mingw / Boost / C++ - 使用 Boost Locale 时出现链接错误(对 __imp_GetACP 的未定义引用) - Mingw / Boost / C++ - link error (undefined reference to __imp_GetACP) when using Boost Locale MinGW gtk未定义参考 - MinGW gtk undefined reference MinGW未定义对“ WsaIoctl”的引用 - MinGW undefined reference to `WsaIoctl' MinGW-w64 gcc上对boost :: random :: random_device构造函数和析构函数的未定义引用 - Undefined reference to boost::random::random_device constructor and destructor on MinGW-w64 gcc MinGW/CMake 对 ZLib 的未定义引用 - MinGW/CMake Undefined Reference to ZLib Mingw和libraw导致未定义引用 - Mingw and libraw results in undefined reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM