【问题标题】:Linux CMake build dynamic library without linking depedenciesLinux CMake 构建动态库而不链接依赖项
【发布时间】:2020-02-04 02:37:34
【问题描述】:

我在 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? 感谢您的支持。

【问题讨论】:

  • 我需要更多信息。您的库是否直接或间接依赖于 openSSL?您图书馆的代码有类似 #include <openssl/ssl.h> 的东西吗?如果后者的答案是肯定的,那么它是一个直接依赖,那么问题是你如何在没有它的情况下链接主机环境?
  • 你能提供你的 CMakeLists.txt 吗?这将有很大帮助。
  • 也许您的 libic 库代码使用 dlopen() 加载 openSSL 并使用 dlclose() 卸载它?这意味着运行时链接而不是构建时。也许这就是你想要的,但在这种情况下,错误会发生在运行时,当找不到库时。
  • 谢谢大家,我找到了答案,链接器问题是因为这个链接器标志-Wl,--no-undefined。只需删除此链接器选项即可

标签: linux cmake openssl shared-libraries cross-compiling


【解决方案1】:

如果您的库依赖于 libssl,则如果不链接到 libssl,则无法编译它。您是否尝试为 arm 架构获取 libssl 包?在我的头顶上,有些东西在

dpkg --add-architecture arm
apt update
apt install libssl-dev:arm

或许

dpkg --add-architecture arm64
apt update
apt install libssl-dev:arm64

希望这会有所帮助!

【讨论】:

  • OP 已经解决了为他的构建查找和安装 openSSL 的问题。你没注意到-- 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")这行吗?
  • 如果 .so 是为其他架构编译的,它将无法找到符号。 CMake 可以为非 arm 架构找到 libcrypto.so,因此无法链接。没有 CMakeLists.txt 就很难说
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多