从其对象文件与-fPIC链接的静态库创建共享对象 [英] Creating shared object from static library whose object files were linked with -fPIC

查看:193
本文介绍了从其对象文件与-fPIC链接的静态库创建共享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我们试图创建一个共享对象文件,用于导出 libname.exports 中指定的一组函数。当然我们知道, .so 文件链接的目标文件必须使用 -fPIC 来创建,所以一直照顾。然后,我们将这些目标文件合并到名为 libname.a 的存档中。现在这应该是创建 .so 文件的基础 - 或者是想法。



我们'将 libname.exports 传递给 - retain-symbols-file ,所以预期的行为是链接器会拉入与这些符号相关的任何 .a 成员。



但是, nm libname.so 为空。另一方面,在 nm libname.a 中进行擦除显示在 libname.exports 中指定的相关符号存在于 .a 成员。



现在我偶然发现 - whole-archive ,从而调整命令行:

  gcc -o libname.so -shared -Wl,-z,defs - 保留符号文件,libname.exports,-L。 libname.a -lc 

到:

  gcc -o libname.so -shared -Wl,-z,defs, -  retain-symbols-file,libname.exports,-L。, -  whole-archive,libname。 a, -  no-whole-archive -lc 

这似乎具有包含 all 来自 .a 的目标文件(尽管大小差异很奇怪)。然而, nm libname.so 仍然没有输出。



如何使用存档文件创建只有 libname.exports 可见的



中指定的符号的共享对象不幸的是如何从静态库创建共享对象文件并不完全回答我的问题。



注意:之前你问。使用 .a 文件作为输入的想法是因为它可以很容易地在 GNUmakefile 中使用模式规则,并且因为无论如何都需要带有 -fPIC .a 文件。链接单个目标文件和存档文件之间不应该有任何区别。 您可以使用 -u SYMBOL 选项强制从档案中读入对象。

 % cc -c -fPIC ac 
%nm ao
00000000 T a
%ar rv liba.a ao
ar:创建liba.a
a - ao
%gcc -o liba.so -shared -ua liba.a
%nm liba.so | awk'$ 3 ==a{print}'
0000042c T a

检查将使用 - retain-symbols-file 指定符号的拼写。例如,从C ++代码编译的对象中的符号名称可能会被破坏:

 %g ++ -c -fPIC ac 
%nm ao | awk'$ 2 ==T{print}'
00000000 T _Z1av


For a project we are trying to create a shared object file that exports a set of functions specified in libname.exports. Of course we know that the object files from which the .so file gets linked have to be created using -fPIC, so that has been taken care of. We then combined the object files into an archive named libname.a. This should now be the basis for the .so file to be created - or so was the idea.

We're passing libname.exports to --retain-symbols-file, so the expected behavior was that the linker would pull in any of the .a members relevant to those symbols.

However, the output of nm libname.so is empty. On the other hand grepping in nm libname.a shows that the relevant symbols named in libname.exports exist in the .a members.

Now I stumbled over --whole-archive and thus adjusted the command line from:

gcc -o libname.so -shared -Wl,-z,defs,--retain-symbols-file,libname.exports,-L. libname.a -lc

to:

gcc -o libname.so -shared -Wl,-z,defs,--retain-symbols-file,libname.exports,-L.,--whole-archive,libname.a,--no-whole-archive -lc

which appears to have the intended effect of including all the object files from the .a (although the size difference is strange). However, nm libname.so still gives me no output.

How can I use the archive file to create a shared object with only the symbols named in libname.exports visible?

Unfortunately How to create a shared object file from static library doesn't quite answer my question.

Note: before you ask. The idea behind using the .a file as input is because it makes it easy to use a pattern rule in GNUmakefile and because the .a file with -fPIC is needed regardless. There shouldn't be any difference between linking the individual object files versus the archive file.

解决方案

You could use the -u SYMBOL option to force objects to be read in from an archive.

% cc -c -fPIC a.c
% nm a.o
00000000 T a
% ar rv liba.a a.o
ar: creating liba.a
a - a.o
% gcc -o liba.so -shared -u a liba.a
% nm liba.so | awk '$3 == "a" { print }'
0000042c T a

One thing to check would be the spellings of the symbols being specified with --retain-symbols-file. For example, symbol names in objects compiled from C++ code are likely to be mangled:

% g++ -c -fPIC a.c
% nm a.o | awk '$2 == "T" { print }'
00000000 T _Z1av

这篇关于从其对象文件与-fPIC链接的静态库创建共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆