静态编译共享库

编程入门 行业动态 更新时间:2024-10-28 11:34:07
本文介绍了静态编译共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

静态编译共享库

本文发布于:2023-08-07 22:35:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1322188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!