可以建立与静态链接库使用的共享库? [英] Possible to build a shared library with static link used library?

查看:152
本文介绍了可以建立与静态链接库使用的共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以建立一个可执行文件静态链接的gcc:

I can build a executable with gcc with static link:

gcc的-static xxx.c -o XXX

gcc -static xxx.c -o xxx

所以我可以在没有任何外部依赖库运行XXX。

So I can run xxx without any external dependent library.

但是,如果我想建立共享库,而不依赖于externel图书馆是什么?我的意思是我想
共享库的静态链接的externel参考。

But what if I want to build shared library without externel dependent library? which I mean I want the shared library statically linked its externel reference in.

推荐答案

这将工作:

# Generate position independent code (PIC)
gcc -fPIC -c -o xxx.o xxx.c

# Build a shared object and link with static libraries
ld -shared -static -o xxx.so xxx.o

# Same thing but with static libc
ld -shared -static -o xxx.so xxx.o -lc

一个澄清:该-static标志,如果给GCC,被传递给链接器(LD),并告诉它与库的静态版本(.a)中(用-l标志指定)的工作,而不是动态版本(的.so)。

A clarification: the -static flag, if given to gcc, is passed on to the linker (ld) and tells it to work with the static version (.a) of a library (specified with the -l flag), rather than the dynamic version (.so).

另一件事:在我的系统(Debian的)最后一个例子给出的的libc.a ...重新编译-fPIC 的错误。 pretty肯定这意味着,我的系统上我有文件libc.a不是用-fPIC编译。一个的的apt-缓存搜索的libc PIC 的也给予一定的效果但是。

Another thing: On my system (Debian) the last example gives a libc.a ... recompile with -fPIC error. Pretty sure that means that the libc.a I have on my system wasn't compiled with -fPIC. An apt-cache search libc pic did give some results however.

另请参阅:程序库HOWTO ,的 SO:结合的.so库,的 LD(1) GCC(1)

See also: Program Library HOWTO, SO: combining .so libs, ld(1), gcc(1)

这篇关于可以建立与静态链接库使用的共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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