【问题标题】:Dynamic library do not have symbol of static library file动态库没有静态库文件的符号
【发布时间】:2019-08-09 11:27:12
【问题描述】:

动态库(.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 文件的完整符号。

$ 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++ c gcc


【解决方案1】:

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

【讨论】:

  • 问题已解决。谢谢。
猜你喜欢
  • 2014-11-23
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 2021-10-18
相关资源
最近更新 更多