可以建立与静态链接库使用的共享库?

可以建立与静态链接库使用的共享库?

问题描述:

我可以建立一个可执行文件静态链接的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)