首页 > 解决方案 > 动态库没有静态库文件的符号

问题描述

动态库(.so)文件中没有静态库文件或函数的符号。

我正在使用 ubuntu 18.04 和 12.04 系统。我使用 -fPIC 选项从 cpp 文件创建了一个目标文件,而不是创建的静态库 (.a)。之后我使用命令创建了一个动态库:g++ -shared -I(include path) -L(other library path) -l(librarys) -o filename.so -Wl,-soname,filename.so staticlib.a"我显示了在 12.04 ubuntu 系统中创建的符号,但在 18.04 系统中没有。我显示了由 so 文件中没有符号创建的 so 文件。我们可以使用 nm -g filename .so 检查它。

如果我尝试使用 12.04,我会在 18.04 系统中的 nm 命令中得到这种类型的结果,它会为我提供所有 cpp 文件的完整 sysmbols。

$ nm -g libPJ.so
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000201020 B __bss_start
                 w __cxa_finalize
                 w __gmon_start__
0000000000201020 D _edata
0000000000201028 B _end
000000000000052c T _fini
0000000000000410 T _init

标签: c++cgcc

解决方案


从静态库链接共享库时,添加-Wl,-whole-archive链接器标志。来自man ld

  --whole-archive
       For each archive mentioned on the command line after the
       --whole-archive option, include every object file in the archive in
       the link, rather than searching the archive for the required object
       files.  This is normally used to turn an archive file into a shared
       library, forcing every object to be included in the resulting
       shared library.  This option may be used more than once.

     Two notes when using this option from gcc: First, gcc doesn’t know
       about this option, so you have to use -Wl,-whole-archive.  Second,
       don’t forget to use -Wl,-no-whole-archive after your list of
       archives, because gcc will add its own list of archives to your
       link and you may not want this flag to affect those as well.

推荐阅读