动态库没有静态库文件的符号

Dynamic library do not have symbol of static library file

本文关键字:符号 文件 动态 静态      更新时间:2023-10-16

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

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

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

$ 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

从静态库链接共享库时-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.