1

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.

Swapnil
  • 8,201
  • 4
  • 38
  • 57
awatan
  • 1,182
  • 15
  • 33

2 Answers2

3

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

jtaylor
  • 2,389
  • 19
  • 19
  • but wouldnt it make myobject.o an executable? I am compile object files first and then combining them into a share library. – awatan Dec 21 '12 at 12:17
  • @JewelThief: Yes, and linking external libraries is done in that last "combining" step, which is called linking. Yes, also libraries are linked. – datenwolf Dec 21 '12 at 14:13
  • @JewelThief: Also be aware of what jilles answered: It's very likely that this static build is neither PIC nor does it match your target architecture. The best thing to do, would be to rebuild that library yourself, making sure the static build is PIC (yes, also static libraries can be PIC) and matches your target arch. – datenwolf Dec 21 '12 at 14:14
  • to create a shared library instead of an executable you have to add -shared to above lines – jtaylor Dec 22 '12 at 19:34
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.

jilles
  • 10,509
  • 2
  • 26
  • 39
  • Thanks @jilles! I am doing what you said. I looked into makefile of mini xml that comes with its source. I found that the library is already being compiled with -fPIC flag. So when I compile my library with libmxml.a(compiled with -fPIC), It get some undefined mxml symbols when I try to run any application using my library. Now When I copy all the .o files of mxml to the source folder of my directory and compile my library including mxml objects along with my library objects, I am able to run an application built using my library with out depending upon libmxml. – awatan Dec 22 '12 at 18:14