如何将libc.a链接到共享库中

时间:2022-06-03 15:31:48

I have a shared library named nvdebug.so which is result of the compile.

我有一个名为nvdebug.so的共享库,它是编译的结果。

I wanna have .so file linked with libc.a

我想要将.so文件与libc.a链接起来

although I searched almost all google site, I cannot find the solution.

虽然我搜索了几乎所有谷歌网站,但我找不到解决方案。

I thought '--whole-archive' command is fittest way to solve.

我认为'--whole-archive'命令是最适合解决的方法。

If it works well, nvdebug.so file size must increase, but it doesn't work well.

如果它运行良好,nvdebug.so文件大小必须增加,但它不能很好地工作。

gcc -shared -Wl,-soname,nvdebug.so -o nvdebug.so -Wl,--whole-archive -L/usr/lib -lc -Wl,--no-whole-archive

I don't know what I shoud modify, machanism of --whole-archive, and -shared command.

我不知道我应该修改什么,-whole-archive和-shared命令的机制。

I'm really appreciate if you guys help me what to do

如果你们帮我做什么,我真的很感激

1 个解决方案

#1


-1  

You cannot link a static library into a shared library, because shared libraries must contain position-independent code, and static libraries contain position-dependent code (called "relocatable code"). You will get a linker error at best. So, this is impossible and the only option is to give up and try something else.

您无法将静态库链接到共享库,因为共享库必须包含与位置无关的代码,而静态库包含与位置相关的代码(称为“可重定位代码”)。您最多会遇到链接器错误。所以,这是不可能的,唯一的选择是放弃并尝试别的东西。

Note

It is actually possible to compile a static library with position-independent code, which would allow you to do this, but this means recompiling libc.a which I'm assuming is a non-option. Just add -fPIC to the CFLAGS when compiling libc, I think.

实际上可以编译一个与位置无关的代码的静态库,这将允许你这样做,但这意味着重新编译libc.a,我假设它是一个非选项。我想,在编译libc时,只需将-fPIC添加到CFLAGS中。

#1


-1  

You cannot link a static library into a shared library, because shared libraries must contain position-independent code, and static libraries contain position-dependent code (called "relocatable code"). You will get a linker error at best. So, this is impossible and the only option is to give up and try something else.

您无法将静态库链接到共享库,因为共享库必须包含与位置无关的代码,而静态库包含与位置相关的代码(称为“可重定位代码”)。您最多会遇到链接器错误。所以,这是不可能的,唯一的选择是放弃并尝试别的东西。

Note

It is actually possible to compile a static library with position-independent code, which would allow you to do this, but this means recompiling libc.a which I'm assuming is a non-option. Just add -fPIC to the CFLAGS when compiling libc, I think.

实际上可以编译一个与位置无关的代码的静态库,这将允许你这样做,但这意味着重新编译libc.a,我假设它是一个非选项。我想,在编译libc时,只需将-fPIC添加到CFLAGS中。