首页 > 解决方案 > Linux CMake 构建动态库而不链接依赖项

问题描述

我在 Linux 上编写一个 C++ 库,库名称是 libic。libic 使用 openssl。当我在主机 Ubuntu 18.04 上构建 libic 时,没有发生错误。但是当我为arm交叉编译libic时,构建libic共享目标时出现链接器错误。错误如下:

....
[100%] Linking CXX shared library libic.so
uclient.c:(.text+0xd96): undefined reference to `event_del'
uclient.c:(.text+0xd9e): undefined reference to `event_free'
uclient.c:(.text+0xdae): undefined reference to `event_del'
uclient.c:(.text+0xdb6): undefined reference to `event_free'
uclient.c:(.text+0xdf8): undefined reference to `SSL_get_shutdown'
uclient.c:(.text+0xe0a): undefined reference to `SSL_free'
uclient.c:(.text+0xe5c): undefined reference to `SSL_free'
uclient.c:(.text+0xe90): undefined reference to `event_base_free'
uclient.c:(.text+0xe98): undefined reference to `pthread_cancel'
uclient.c:(.text+0xed6): undefined reference to `SSL_get_shutdown'
uclient.c:(.text+0xefa): undefined reference to `SSL_set_shutdown'
uclient.c:(.text+0xf06): undefined reference to `SSL_shutdown'
uclient.c:(.text+0xf12): undefined reference to `SSL_set_shutdown'
uclient.c:(.text+0xf1a): undefined reference to `SSL_shutdown'

配置日志如下

-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE  
-- Found OpenSSL: /home/drone/tizen-studio/platforms/tizen-3.0/mobile/rootstraps/mobile-3.0-device.core/usr/lib/libcrypto.so (found version "1.0.2k") 
-- Using OpenSSL 1.0.2k
-- Found CURL: /home/drone/tizen-studio/platforms/tizen-3.0/mobile/rootstraps/mobile-3.0-device.core/usr/lib/libcurl.so (found version "7.50.2")
-- 
--   C/C++:
--     C++ Compiler:                /home/drone/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-g++ (ver 7.3.1)
--     C++ flags (Release):         -mthumb  -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi -std=c++1y -fPIC -O3 -DNDEBUG
--     C++ flags (Debug):           -mthumb  -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi -std=c++1y -fPIC -g
--     C Compiler:                  /home/drone/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-gcc
--     C flags (Release):           -mthumb  -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi -fPIC -O3 -DNDEBUG
--     C flags (Debug):             -mthumb  -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi -fPIC -g
--   CMAKE_INSTALL_PREFIX:          /home/drake/Documents/code/libscs/packages/libscs
--     Linker flags (Release):      -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now 
--     Linker flags (Debug):        -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now 

似乎使用交叉编译,libic 需要与 openssl 链接。我想问一下如何在不与openssl链接的情况下构建libic shared?谢谢你的支持。

标签: linuxcmakeopensslshared-librariescross-compiling

解决方案


谢谢大家,我找到了答案,链接器问题是因为这个链接器标志-Wl,--no-undefined。只需删除此链接器选项即可


推荐阅读