简体   繁体   中英

Compiling PIC object with static library

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM