如何将共享对象中的所有对象归档

如何将共享对象中的所有对象归档

本文介绍了如何将共享对象中的所有对象归档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译我们的项目时,我们创建了几个档案(静态库),比如 liby.a libz.a 每个都包含一个定义函数 y_function() z_function()的目标文件。然后,这些档案会被加入一个共享对象,比如 libyz.so ,这是我们的主要可分配目标之一。

  g ++ -fPIC -c -o yo y.cpp 
ar cr liby.a yo
g ++ -fPIC -c -o zo z.cpp
ar cr libz.a zo
g ++ -shared -L。 -ly -lz -o libyz.so

在示例程序中使用此共享对象时,请说 xc ,链接失败是因为对函数 y_function() z_function()

  g ++ xo -L。然而,当我将最终的可执行文件直接链接到存档(静态库)时,它可以正常工作。 )。

  g ++ xo -L。 -ly -lz -o xyz 

我的猜测是存档中包含的目标文件不是链接到共享库中,因为它们不在其中。如何强制包含?

编辑:

包含可以强制使用--whole-archive ld 选项。但如果导致编译错误:

  g ++ -shared'-Wl, -  whole-archive'-L。 -ly -lz -o libyz.so 
/usr/lib/libc_nonshared.a(elf-init.oS):在函数`__libc_csu_init'中:
(.text + 0x1d):undefined指向` __init_array_end'
/ usr / bin / ld:/usr/lib/libc_nonshared.a(elf-init.oS):在创建共享对象$ b $时不能使用针对未定义隐藏符号__init_array_end的R_X86_64_PC32 b / usr / bin / ld:最终链接失败:错误的值
解决方案

您可以尝试(ld(2)):

  --whole-archive 
对于在命令行中提到的每个归档文件--whole-archive选项后,将每个目标文件包含在链接中的
归档文件中,而不是在存档中搜索所需的目标文件。这通常用于将
归档文件转换为共享库,强制将每个对象包含在生成的共享库中。这
选项可能会多次使用。

(gcc -Wl, - 整个存档)



另外,您应该在库列表的末尾放置 -Wl, - no-whole-archive 。 (正如德米特里尤达科夫在下面的评论中所说的)

When compiling our project, we create several archives (static libraries), say liby.a and libz.a that each contains an object file defining a function y_function() and z_function(). Then, these archives are joined in a shared object, say libyz.so, that is one of our main distributable target.

g++  -fPIC  -c -o y.o y.cpp
ar cr liby.a y.o
g++  -fPIC  -c -o z.o z.cpp
ar cr libz.a z.o
g++ -shared -L. -ly -lz -o libyz.so

When using this shared object into the example program, say x.c, the link fails because of an undefined references to functions y_function() and z_function().

g++ x.o -L. -lyz -o xyz

It works however when I link the final executable directly with the archives (static libraries).

g++ x.o -L. -ly -lz -o xyz

My guess is that the object files contained in the archives are not linked into the shared library because they are not used in it. How to force inclusion?

Edit:

Inclusion can be forced using --whole-archive ld option. But if results in compilation errors:

g++ -shared '-Wl,--whole-archive' -L. -ly -lz -o libyz.so
/usr/lib/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x1d): undefined reference to `__init_array_end'
/usr/bin/ld: /usr/lib/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

Any idea where this comes from?

解决方案

You could try (ld(2)):

   --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.

(gcc -Wl,--whole-archive)

Plus, you should put -Wl,--no-whole-archive at the end of the library list. (as said by Dmitry Yudakov in the comment below)

这篇关于如何将共享对象中的所有对象归档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:55