简体   繁体   English

链接到共享库时,不会导出 static 库中的符号

[英]Symbols from a static library are not exported while linking to a shared library

Code for a shared library is modular, consists of several independent units.共享库的代码是模块化的,由几个独立的单元组成。 Each unit is built into a static library.每个单元都内置在 static 库中。


unit1.c单元1.c

#include <stdio.h>

void HelloWorld() {
  printf("Hello World!\n");
}

unit2.c单元2.c

#include <stdio.h>

void GoodbyeWorld() {
  printf("Goodbye World!\n");
}

CMakeLists.txt CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)

add_library(unit1 STATIC unit1.c)
target_compile_options(unit1 PRIVATE -fPIC)

add_library(unit2 STATIC unit2.c)
target_compile_options(unit2 PRIVATE -fPIC)

add_library(merged SHARED)
target_link_libraries(merged unit1 unit2)
set_target_properties(merged PROPERTIES LINKER_LANGUAGE C)

Build steps:构建步骤:

cmake . && cmake --build .

Exported symbols by libmerged.so: libmerged.so 导出的符号:

$ nm -D --defined-only libmerged.so 
0000000000201020 B __bss_start
0000000000201020 D _edata
0000000000201028 B _end
00000000000005a0 T _fini
0000000000000458 T _init

Q Why symbols HelloWorld and GoodbyeWorld are not exported? Q为什么符号 HelloWorld 和 GoodbyeWorld 没有导出? How to fix it?如何解决?

  • I've tried --version-script without success.我试过--version-script没有成功。

    Additional setting in CMakeLists.txt CMakeLists.txt 中的附加设置

    set_target_properties(merged PROPERTIES LINK_FLAGS -Wl,--version- script=merged.version)

    merged.version合并版本

    merged { global: HelloWorld; GoodbyeWorld; local: *; };
  • Also tried force load static libaries without success还尝试强制加载 static 库但未成功
    set_target_properties(merged PROPERTIES LINK_FLAGS -Wl,-force_load,libunit1.a)

You need to pass the --whole-archive option to the linker.您需要将 --whole --whole-archive选项传递给 linker。 In CMake, you can do it as follows.在 CMake 中,可以按如下方式进行。

CMakeLists.txt CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)

add_library(unit1 STATIC unit1.c)
target_compile_options(unit1 PRIVATE -fPIC)

add_library(unit2 STATIC unit2.c)
target_compile_options(unit2 PRIVATE -fPIC)

add_library(merged SHARED)
set_target_properties(merged PROPERTIES LINKER_LANGUAGE C)
target_link_libraries(merged
        "-Wl,--whole-archive libunit1.a libunit2.a -Wl,--no-whole-archive"
        unit1 unit2
)

Note: The target_link_libraries command can be used to specify linker flags as well, not only library names.注意: target_link_libraries命令也可用于指定 linker 标志,而不仅仅是库名称。 The quotes are important, otherwise CMake might rearrange the flags and remove duplicates.引号很重要,否则 CMake 可能会重新排列标志并删除重复项。

Exported symbols导出的符号

$ nm libmerged.so | grep " T "
000000000000065d T GoodbyeWorld
000000000000064a T HelloWorld
0000000000000670 T _fini
0000000000000520 T _init

Another option, to avoid the problem, would be to create OBJECT instead of STATIC libraries for unit1 and unit2 .为避免该问题,另一种选择是为unit1unit2创建OBJECT而不是STATIC库。

CMakeLists.txt CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)

add_library(unit1 OBJECT unit1.c)
target_compile_options(unit1 PRIVATE -fPIC)

add_library(unit2 OBJECT unit2.c)
target_compile_options(unit2 PRIVATE -fPIC)

add_library(merged SHARED $<TARGET_OBJECTS:unit1> $<TARGET_OBJECTS:unit2>)
set_target_properties(merged PROPERTIES LINKER_LANGUAGE C)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止静态库中的所有符号加载以及链接静态库时为什么导出来自同一.o文件的其他符号以进行测试 - How to prevent all symbols from static library to load and why other symbols from same .o file get exported to test while linking static library static 和共享库及其交叉链接 - static and shared library and their cross linking 使用静态库编译的共享库中的未定义符号 - Undefined symbols in a shared library compiled with a static library 将共享库与静态库链接 - Linking shared library against static library LD:将共享库链接到静态库 - LD: Linking a shared library to a static library dll链接静态库-未使用函数中未解析的链接器符号 - dll linking static library - unresolved linker symbols from unused functions 从静态库中剥离所有符号是否会阻止链接? - Does stripping all symbols from a static library prevent linking? 静态函数和变量在共享库中导出 - Static function and variable get exported in shared library 链接从静态库中链接符号的动态库:macOS 与 Linux - Linking a dynamic library that links in symbols from a static library: macOS vs Linux 已定义符号的静态库链接问题“未定义符号” - Static Library Linking Issue "Undefined symbols" for symbols that are defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM