Linking partially static and partially dynamic in GCC

给你一囗甜甜゛ 提交于 2019-12-06 04:03:08
Z boson

The following worked for me

ln -s `gcc -print-file-name=libc.a`
gcc -static-libgcc -L. -lc test.c

Then ldd a.out gives:

not a dynamic executable

Edit:

The OP wants to link one library dynamically and another statically. He have the example of linking libc statically and libm dynamically. That particular case I have not been able to achieve. However, the opposite is possible i.e. linking libc dynamically and libm statically.

ln -s `gcc -print-file-name=libm.a`
gcc test.c -L. -lm

then ldd a.out gives

libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0x41960000)
/lib/ld-linux.so.2 (0x4193d000)

Note that the link order matters.e.g gcc -L. -lm test.c does not work.

This works with other libraries as well. For example gomp

gcc -fopenmp test.c

ldd shows libgomp.so.1. We can link it statically like this

ln -s `gcc -print-file-name=libgomp.a`
gcc -L. -fopenmp test.c

Now ldd a.out does not show libgomp.so.1. But in this case pthreads is still linked dynamically. To link pthreads statically requires that libc be linked statically as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!