Link 定位到图书馆

Link target to libraries

我有一个 "CMakified" 版本的 CryptoPP,我正在使用 CMake-GUI 创建一个 Ming-make 可以处理的 MakeFile,如下图所示。

"MakeFile"创建成功,但是当我执行mingw32-make.exe时,我在最后出现了以下错误。

Linking CXX executable cryptest.exe
CMakeFiles\cryptest.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x8e82): undefined reference to `setsockopt@20'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x5cb): undefined reference to `socket@12'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x643): undefined reference to `closesocket@4'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x6f0): undefined reference to `htonl@4'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x70a): undefined reference to `inet_addr@4'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x759): undefined reference to `htons@4'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x7f4): undefined reference to `bind@12'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x85b): undefined reference to `listen@8'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x909): undefined reference to `inet_addr@4'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x922): undefined reference to `gethostbyname@4'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x978): undefined reference to `htons@4'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0xa18): undefined reference to `connect@12'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0xaac): undefined reference to `accept@12'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0xb58): undefined reference to `getsockname@12'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0xbc6): undefined reference to `getpeername@12'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0xc87): undefined reference to `send@16'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0xd83): undefined reference to `recv@16'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0xe25): undefined reference to `shutdown@8'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0xe96): undefined reference to `ioctlsocket@12'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0xfbb): undefined reference to `select@20'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x1010): undefined reference to `select@20'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x11a1): undefined reference to `select@20'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x11f6): undefined reference to `select@20'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x133f): undefined reference to `getservbyname@8'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x1407): undefined reference to `ntohs@4'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x1526): undefined reference to `WSAStartup@8'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x16cf): undefined reference to `WSACleanup@0'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x17fb): undefined reference to `WSAGetLastError@0'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x180f): undefined reference to `WSASetLastError@4'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x1cfc): undefined reference to `WSARecv@28'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x1d27): undefined reference to `WSAGetLastError@0'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x1f61): undefined reference to `WSAGetOverlappedResult@20'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x1f8c): undefined reference to `WSAGetLastError@0'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x2397): undefined reference to `WSASend@28'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x23c1): undefined reference to `WSAGetLastError@0'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x24cc): undefined reference to `WSAEventSelect@12'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x2587): undefined reference to `WSAEnumNetworkEvents@12'
libcryptopp.a(socketft.cpp.obj):socketft.cpp:(.text+0x2a01): undefined reference to `WSAGetOverlappedResult@20'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [cryptest.exe] Error 1
mingw32-make[1]: *** [CMakeFiles/cryptest.dir/all] Error 2
mingw32-make: *** [all] Error 2

我尝试将 LDLIBS += -lws2_32 添加到 make 文件的末尾,但这没有帮助。我猜这些错误是因为 make 文件中没有提到目标 link 库,但我不确定该怎么做。

CMakeLists.txt 应该调用

target_link_libraries(cryptopp ws2_32)

在命令 ADD_LIBRARY 之后解决未定义的引用。

在听了 Kieth 上面的评论之后,我发现 LIBS 和 LDLIBS 都不被 CMAKE 认可。要使用的变量是 CXX_STANDARD_LIBRARIES。我在 CMAKE 高级选项中做了以下更改:


我在最后添加了 -lws2_32 然后生成了 "MakeFile" .
这解决了问题。