静态编译共享库 [英] Compile a shared library statically

查看:80
本文介绍了静态编译共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了一些自制的功能,这是我编到我的其他程序共享库,但我不得不结束与节目我都用来编译静态库中的所有库链​​接。下面是一个例子:

I've got a shared library with some homemade functions, which I compile into my other programs, but I have to link the end program with all the libraries I have used to compile the static library. Here is an example:

我有这需要一个函数从另一个库 libbar.so 库函数

I have function foo in the library which requires a function from another library libbar.so.

在我的主程序使用功能我与 -lbar 标志进行编译。有没有一种方法我可以编译的我的的静态库,因此包括从其他图书馆所需的所有code,我可以编译我的最终方案,而无需在 -lbar 标志?

In my main program to use function foo I have to compile it with the -lbar flag. Is there a way I can compile my library statically so it includes all the required code from the other libraries, and I can compile my end program without needing the -lbar flag?

推荐答案

共享对象(.so)的不是图书馆,这些对象。你不能提取其中的一部分,其他图书馆将其插入。

Shared objects (.so) aren't libraries, they are objects. You can't extract part of them and insert it in other libraries.

如果构建一个共享对象,它引用了其他的你可以做些什么 - 但其他将在运行时需要。只需连接libfoo的时候添加-lbar。

What you can do if build a shared object which references the other -- but the other will be needed at run time. Just add the -lbar when linking libfoo.

如果您能够建立libbar的,你可以明显地使库是libfoo的和libbar的组合。 IIRC,你还可以使连接器建立一个库是libfoo的,并通过链接意味着libbar的去的.o以.a libbar的的需要的一部分。例如:

If you are able to build libbar, you can obviously make a library which is the combination of libfoo and libbar. IIRC, you can also make the linker build a library which is libfoo and the needed part of libbar by linking a .a with the .o meant to go in libbar. Example:

gcc -fPIC -c lib1.c     # define foofn(), reference barfn1()
gcc -fPIC -c lib2a.c    # define barfn1(), reference barfn2()
gcc -fPIC -c lib2b.c    # define barfn2()
gcc -fPIC -c lib2c.c    # define barfn3()
gcc -c main.c           # reference foofn()
ar -cru libbar.a lib2*.o
gcc -shared -o libfoo.so lib1.o -L. -lbar
nm libfoo.so | grep barfn2()    # ok, not here
gcc -o prog main.o -L. -lfoo
env LD_LIBRARY_PATH=. ./prog    # works, so foofn(), barfn1() and barfn2() are found

这篇关于静态编译共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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