简体   繁体   中英

Linker errors from CMake script compiling project using Github repository code

I've cloned this Github project:

https://github.com/machinezone/IXWebSocket

pasted the example code to my own empty project, main.cpp (pasted below) and written a basic CMakeLists.txt (pasted below).

The cloned Github project compiles/links fine and outputs a library: IXWebSocket/build/libixwebsocket.a

The example code (now within my project) compiles but I'm getting problems with the linking. I used:

target_link_libraries(IXWebSocketTest <path/to/code>/IXWebSocket/build/libixwebsocket.a)

I'm currently getting this error as if the linking isn't correct:

/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: 

My 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)

UPDATE

CMake file has changed to this:

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) 

giving this command line:

/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/libcrypto.so -Wl,-Bstatic -lixwebsocket -Wl,-Bdynamic /usr/lib/x86_64-linux-gnu/libz.so

which is resulting in:

/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'

Short answer

build libixwebsocket as shared library cmake -DBUILD_SHARED_LIBS=ON ...

Longer answer

When you build a static library, its dependencies aren't actually built into the library. The compiler just makes marks that the dependencies will be provided later on the linkage stage. So, it's assumed you will provide them along with the library on the linkage stage. Possible solutions are

  • build the library as shared
  • connect IXWebSocket to your project as a dependency and build libixwebsocket as an intermediate step, CMake take care of dependencies then.

UPD

From documentation of IXWebSocket

$ clang++ --std=c++11 --stdlib=libc++ main.cpp -lixwebsocket -lz -framework Security -framework Foundation

So, it's assumed you should link against zlib . Your CMakeLists.txt should look like

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) 

Also, you probably will have to enable c++11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM