我克隆了这个Github项目:
https://github.com/machinezone/IXWebSocket
将示例代码粘贴到我自己的空项目main.cpp (粘贴在下面),并编写了一个基本的CMakeLists.txt (粘贴在下面)。
克隆的Github项目编译/链接良好,并输出一个库:IXWebSocket/build/libixwebsocket.a
示例代码(现在在我的项目中)编译,但我在链接方面遇到了问题。我用:
target_link_libraries(IXWebSocketTest <path/to/code>/IXWebSocket/build/libixwebsocket.a)我目前正在收到这个错误,好像链接不正确一样:
/usr/bin/ld: /usr/bin/ld: DWARF error: invalid or unhandled FORM value: 0x23
IXWebSocket/build/libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateCompressor::~WebSocketPerMessageDeflateCompressor()':
IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x78): undefined reference to `deflateEnd'
/usr/bin/ld: 我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.2)
project(IXWebSocketTest)
set(CMAKE_BUILD_TYPE Release)
add_executable(IXWebSocketTest main.cpp)
include_directories("<path/to/code>/IXWebSocket/")
# I think this line might be wrong?
target_link_libraries(IXWebSocketTest <path/to/code>/IXWebSocket/build/libixwebsocket.a)更新
CMake文件已更改为:
add_executable(IXWebSocketTest main.cpp)
include_directories(<path>/IXWebSocket/")
find_package(ZLIB)
find_package(OpenSSL 1.1 REQUIRED)
include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR})
target_link_libraries(IXWebSocketTest OpenSSL::SSL OpenSSL::Crypto libixwebsocket.a ZLIB::ZLIB) 给出这个命令行:
/usr/bin/c++ -O3 -DNDEBUG -rdynamic CMakeFiles/IXWebSocketTest.dir/main.cpp.o -o IXWebSocketTest /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/x86_64-linux-gnu/libcryp.so -Wl,-Bstatic -lixwebsocket -Wl,-Bdynamic /usr/lib/x86_64-linux/libz.so
其结果是:
/usr/bin/ld: /usr/local/lib/libixwebsocket.a(IXSocketOpenSSL.cpp.o): in function `ix::SocketOpenSSL::openSSLInitialize()':
IXSocketOpenSSL.cpp:(.text+0x209): undefined reference to `OPENSSL_init_ssl'
/usr/bin/ld: IXSocketOpenSSL.cpp:(.text+0x221): undefined reference to `OPENSSL_init_ssl'
/usr/bin/ld: IXSocketOpenSSL.cpp:(.text+0x230): undefined reference to `OPENSSL_init_ssl'
/usr/bin/ld: /usr/local/lib/libixwebsocket.a(IXSocketOpenSSL.cpp.o): in function `ix::SocketOpenSSL::getSSLError[abi:cxx11](int)':
IXSocketOpenSSL.cpp:(.text+0x2fc): undefined reference to `SSL_get_error'
/usr/bin/ld: IXSocketOpenSSL.cpp:(.text+0x38e): undefined reference to `ERR_get_error'
/usr/bin/ld: IXSocketOpenSSL.cpp:(.text+0x3dc): undefined reference to `ERR_error_string'
/usr/bin/ld: IXSocketOpenSSL.cpp:(.text+0x49c): undefined reference to `ERR_get_error'
/usr/bin/ld: IXSocketOpenSSL.cpp:(.text+0x4e3): undefined reference to `ERR_error_string'发布于 2022-07-07 21:49:29
短答案
将libixwebsocket构建为共享库cmake -DBUILD_SHARED_LIBS=ON ...
更长的答案
当您构建静态库时,它的依赖项实际上并不是内置到库中的。编译器只是标记依赖关系将在稍后的链接阶段提供。因此,假设您将在链接阶段与库一起提供它们。可能的解决办法是
IXWebSocket连接到您的项目作为依赖项,而构建libixwebsocket作为中间步骤,CMake负责处理依赖关系。UPD
来自IXWebSocket的文档
$ clang++ --std=c++11 --stdlib=libc++ main.cpp -lixwebsocket -lz -framework Security -framework Foundation因此,假设您应该链接到zlib。你的CMakeLists.txt应该看起来像
cmake_minimum_required(VERSION 3.2)
project(IXWebSocketTest)
set(CMAKE_BUILD_TYPE Release)
add_executable(IXWebSocketTest main.cpp)
include_directories("<path/to/code>/IXWebSocket/")
find_package(ZLIB)
# I think this line might be wrong?
target_link_libraries(IXWebSocketTest <path/to/code>/IXWebSocket/build/libixwebsocket.a ZLIB::ZLIB) 而且,您可能需要启用c++11
https://stackoverflow.com/questions/72904377
复制相似问题