将档案 (.a) 链接到共享对象 (.so)

Linking archives (.a) into shared object (.so)(将档案 (.a) 链接到共享对象 (.so))

本文介绍了将档案 (.a) 链接到共享对象 (.so)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些共享对象文件编译成 archive.a:

I'm compiling some shared objects file into an archive.a:

$ g++ -c -Iinclude/ -fPIC -O0 -o object1.o source1.cpp
$ g++ -c -Iinclude/ -fPIC -O0 -o object2.o source2.cpp
$ ar rvs archive.a object1.o object2.o
r - object1.o
r - object2.o

到目前为止一切顺利.生成的 archive.a 有一些 KB 大小.带有 nm 的转储显示相应的目标文件包含在文件中.

So far so good. The resulting archive.a has a good size of some KB. A dump with nm shows that the corresponding object-files are contained within the files.

现在我想将这些档案中的几个编译成一个共享对象文件.

Now I'm wanting to compile several of these archives into a shared object file.

g++ -g -O0 -Iinclude/ -I/usr/include/somelibrary -shared -o libLibrary.so archive1.a archive2.a

结果是我生成的库文件几乎是:

The result is that my resulting library file is nearly empty:

$ nm -D libLibrary.so
                 w _Jv_RegisterClasses
0000000000201010 A __bss_start
                 w __cxa_finalize
                 w __gmon_start__
0000000000201010 A _edata
0000000000201020 A _end
0000000000000578 T _fini
0000000000000430 T _init

知道我做错了什么吗?

当我尝试切换 -Wl,--whole-archive 时,会发生以下情况:

When I try the switch -Wl,--whole-archive, following happens:

/usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0xd): undefined reference to `__init_array_end'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS): relocation R_X86_64_PC32 against undefined hidden symbol `__init_array_end' can not be used when making a shared object
/usr/bin/ld: final link failed: Bad value
collect2: ld returned 1 exit status
make: *** [libKeynect.so] Error 1

推荐答案

.a 文件中未使用的符号/目标文件将被链接器丢弃.

symbols/object files in .a files that's not used, will be discarded by the linker.

使用 -Wl,--whole-archive 进行链接以包含整个 .a 文件编辑,您还需要在指定库之后添加 -Wl,--no-whole-archive ,因此整个内容将是 -Wl,--whole-archivearchive1.a archive2.a -Wl,--no-whole-archive

Use -Wl,--whole-archive for the linking to include the entire .a file Edit, you'll need to add -Wl,--no-whole-archive after you specify your library as well, so the whole thing will be -Wl,--whole-archive archive1.a archive2.a -Wl,--no-whole-archive

这篇关于将档案 (.a) 链接到共享对象 (.so)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将档案 (.a) 链接到共享对象 (.so)

基础教程推荐