如何在gcc中将静态库链接到动态库中

时间:2023-01-13 16:14:24

Under gcc (g++), I have compiled a static .a (call it some_static_lib.a) library. I want to link (is that the right phrase?) this .a file into another dynamic library (call it libsomeDyn.so) that I'm building. Though the .so compiles, I don't see content of .a under .so using nm command:

在gcc (g++ +)下,我编译了一个静态的。我想把这个文件链接到我正在构建的另一个动态库(称为libsomeDyn.so)中。虽然.so编译了,但是我没有看到a下面的内容。所以使用nm命令:

/usr/bin/g++ -fPIC -g -O2 -Wall -Werror -pipe -march=pentium3 -mtune=prescott -MD -D_FILE_OFFSET_BITS=64 -DLINUX -D_GNU_SOURCE -D_THREAD_SAFE -I../../../../../../../../ -I../../../../../../../..//libraries -Wl,-rpath,/usr/lib -o libsomeDyn.so some.o another.o some_static_lib.a -shared -Wl -x -Wl,-soname,libsomeDyn.so

/usr/bin/g + + - fpic - g - 02 - wall -Werror管3月= pentium3 - mtune =普雷斯科特md - d_file_offset_bits = 64 -DLINUX -D_GNU_SOURCE -D_THREAD_SAFE我. . / . . / . . / . . / . . / . . / . . / . ./我. . / . . / . . / . . / . . / . . / . . / . ./ /库- wl,rpath、/ usr / lib - o libsomeDyn。所以一些。另一个。o some_static_lib。a -Wl -x -Wl,-soname,libsomeDyn.so。

I do not see functions under some_static_lib.a under libsomeDyn.so. What am I doing wrong?

我在some_static_lib下看不到函数。一个libsomeDyn.so之下。我做错了什么?

2 个解决方案

#1


35  

Static libraries have special rules when it comes to linking. An object from the static library will only be added to the binary if the object provides an unresolved symbol.

当涉及到链接时,静态库有特殊的规则。静态库中的对象只有在对象提供未解析符号时才会添加到二进制文件中。

On Linux, you can change that behavior with the --whole-archive linker option:

在Linux上,您可以使用—全归档链接器选项更改这种行为:

g++ -Wl,--whole-archive some_static_lib.a -Wl,--no-whole-archive

#2


14  

For every one that comes across that problem like me (and has not understand the answer properly): here is a short howto generate a dynamic library (libmylib.so) from a static one (mylib.a):

对于每个遇到像我这样的问题的人(而且还没有正确地理解答案):这里有一个从静态库(mylib.a)生成动态库(libmylib.so)的简短方法:

1.) create a mylib.c file that only imports the mylib.h file

1)创建一个mylib。只导入mylib的c文件。h文件

2.) compile this mylib.c to mylib.o with

2)编译这个mylib。mylib c。与阿

gcc -c -fPIC mylib.c -o msat.o

3.) generate a dynamic library with the following command:

3.)生成具有以下命令的动态库:

gcc --whole-archive -shared -Wl,-soname,libmylib.so -o libmylib.so mylib.o mylib.a 

That worked at least for me, turning a static library (compiled with -fPIC) to a dynamic library. I'm not sure wether this will work for other libraries to.

这至少对我起了作用,将静态库(用-fPIC编译)转换为动态库。我不确定这是否适用于其他图书馆。

#1


35  

Static libraries have special rules when it comes to linking. An object from the static library will only be added to the binary if the object provides an unresolved symbol.

当涉及到链接时,静态库有特殊的规则。静态库中的对象只有在对象提供未解析符号时才会添加到二进制文件中。

On Linux, you can change that behavior with the --whole-archive linker option:

在Linux上,您可以使用—全归档链接器选项更改这种行为:

g++ -Wl,--whole-archive some_static_lib.a -Wl,--no-whole-archive

#2


14  

For every one that comes across that problem like me (and has not understand the answer properly): here is a short howto generate a dynamic library (libmylib.so) from a static one (mylib.a):

对于每个遇到像我这样的问题的人(而且还没有正确地理解答案):这里有一个从静态库(mylib.a)生成动态库(libmylib.so)的简短方法:

1.) create a mylib.c file that only imports the mylib.h file

1)创建一个mylib。只导入mylib的c文件。h文件

2.) compile this mylib.c to mylib.o with

2)编译这个mylib。mylib c。与阿

gcc -c -fPIC mylib.c -o msat.o

3.) generate a dynamic library with the following command:

3.)生成具有以下命令的动态库:

gcc --whole-archive -shared -Wl,-soname,libmylib.so -o libmylib.so mylib.o mylib.a 

That worked at least for me, turning a static library (compiled with -fPIC) to a dynamic library. I'm not sure wether this will work for other libraries to.

这至少对我起了作用,将静态库(用-fPIC编译)转换为动态库。我不确定这是否适用于其他图书馆。