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

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

问题描述

我可以使用带有静态链接的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依赖库的共享库?我想要
共享库静态链接其外部引用。

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.

推荐答案

p>

This will work:

# 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

澄清:静态标志,如果给予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 ... recompile with -fPIC 错误。很确定这意味着我在我的系统上的libc.a没有使用-fPIC编译。

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)

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

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