c++ - 动态库没有静态库文件的符号
问题描述
动态库(.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
解决方案
从静态库链接共享库时,添加-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.
推荐阅读
- php - PHP - 下一页的未定义变量
- mysql - MySQL - 使用来自多个表的更新/设置
- javascript - 如何以给定的比例初始化缩放?
- android - Visual Studio, Android: Find out, which Framework is neccessary for Nuget-packages
- java - 在 Selenium 中定位脚本
- java - org.gradle.tooling.BuildException:无法使用 Gradle 分发版“https://services.gradle.org/distributions/gradle-3.1-bin.zip”执行构建
- php - Prestashop - 模块中的 index.php?controller=404
- android - 如何更改在 TargetSDK 21 中完美运行的 TargetSDK 23 的按钮背景?
- python - Python 3:我尝试打开 pdf 时出现消息错误
- c# - 覆盖基类的内部 get-only 属性