Linux 将静态库转换为共享库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/655163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a Static Library to a Shared Library?
提问by Eli Courtwright
I have a third-party library which consists mainly of a large number of static (.a
) library files. I can compile this into a single .a
library file, but I really need it to be a single .so
shared library file.
我有一个第三方库,主要由大量静态 ( .a
) 库文件组成。我可以将其编译为单个.a
库文件,但我确实需要将其作为单个.so
共享库文件。
Is there any way to convert a static .a
file into a shared .so
file? Or more generally is there a good way to combine a huge number of static .a
files with a few .o
object files into a single .so
file?
有没有办法将静态.a
文件转换为共享.so
文件?或者更普遍的是,有没有一种好方法可以将大量静态.a
文件和几个.o
目标文件组合成一个.so
文件?
采纳答案by dicroce
Does this (with appropriate -L's of course)
这样做(当然有适当的 -L)
gcc -shared -o megalib.so foo.o bar.o -la_static_lib -lb_static_lib
Not do it?
不做吗?
回答by vitaly.v.ch
You can't do this if objects within static library was compiled without -fPIC or like.
如果静态库中的对象是在没有 -fPIC 之类的情况下编译的,则无法执行此操作。
回答by Calm
g++ -shared -o megalib.so foo.o bar.o -Wl,--whole-archive -la_static_lib -lb_static_lib -Wl,--no-whole-archive -lc_static_lib -lother_shared_object
I'm not sure about gcc, but for g++ I had to add the --whole-archive linker option to include the objects from the static libraries in the shared object. The --no-whole-archive option is necessary if you want to link to libc_static_lib.a and libother_shared_object.so, but not include them as a whole in megalib.so.
我不确定 gcc,但对于 g++,我必须添加 --whole-archive 链接器选项以将静态库中的对象包含在共享对象中。如果要链接到 libc_static_lib.a 和 libother_shared_object.so,但不将它们作为一个整体包含在 megalib.so 中,则需要 --no-whole-archive 选项。
回答by Anastasios Andronidis
ar -x
can be also useful if you want to focus on specific objects from your .a
s and you don't want to add anything on your own.
ar -x
如果您想专注于.a
s 中的特定对象并且不想自己添加任何内容,则也很有用。
Examples:
例子:
ar -x lib***.a
gcc -shared *.o -o lib***.so
回答by Artur Shaikhullin
ar?-x?lib***.a
gcc?-shared?*.o?-o?lib***.so