Compiling PIC object with static library

╄→гoц情女王★ 提交于 2019-12-08 08:17:03

问题


I am generating a shared library on linux and I am using this xml parser "mini xml" to parse some xml config files for my library. The thing I want is not to have any dependency on this xml parser so I want to use the static libmxml.a library provided by the vendor instead of using libmxml.so which is also there along with libmxml.so such that my shared library does not depend on libmxml when deployed. I have tried following but it doesnt work.

gcc -fPIC -o myobject.o -static -lmxml -c myobject.c

but it gives warning

Linker input unused because linking not done

What am i missing? Any help would be appreciated.


回答1:


You need to build the mxml library specially for this, creating a static library with PIC code (-fPIC), say libmxml_pic.a. The libmxml.a contains position-dependent code which is only suitable for executables (on 32-bit x86 it will work but it is still ugly).

You will also want to avoid exporting the mxml symbols from your library. You can do this with a version script (--version-script to ld, see documentation) and/or by passing -fvisibility=hidden while compiling the mxml objects.




回答2:


You get the warning because -c means only compile. The linker is never run, so -lmxml which is a linker command has no effect.

-static will make the whole executable static, which means also a static libc. This might not be what you want. To only link in libmxml.a statically use:

gcc -fPIC myobject.o -o executable /usr/lib/libmxml.a

or

gcc -fPIC myobject.o -o executable -Wl,-Bstatic -lmxml -Wl,-Bdynamic

to create a shared library instead of an executable add -shared



来源:https://stackoverflow.com/questions/13989745/compiling-pic-object-with-static-library

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