0

I want to use one program as a shared library for an other program.

I started as follows: I have a application which I have compiled using:

/usr/bin/g++ -I/usr/include/libxml2  -Xlinker -zmuldefs -fPIC -c a.cpp
/usr/bin/g++ -I/usr/include/libxml2  -Xlinker -zmuldefs -fPIC -c b.cpp
/usr/bin/g++ -I/usr/include/libxml2  -Xlinker -zmuldefs -fPIC -c c.cpp

Then I have created a shared object library from the objects I get from this file using this command:

g++ -fPIC -Xlinker -zmuldefs -shared -o libabc.so a.o b.o c.o

After this I get the libabc.so file which I copy to the

sudo cp libabc.so /usr/local/lib/libabc.so

Now when I compile my orignal application which will use this newly created library libabc.so using this command:

/usr/local/lib/libabd.so: undefined reference to `xmlXPathNewContext'

I get errors for all the functions I used from the included library libxml2 in the first application and the function which has this undefined reference is actually the library I include in the first program I mean I have tested it. So kindly anyone guide me where I need corrections.

2 Answers2

0

I don't see the command line that you used to link your application against your library, but I suppose that adding -lxml2 to the flags passed to the linker should solve the problem.

Marco Leogrande
  • 8,050
  • 4
  • 30
  • 48
  • We cannot help more without having more details: is `libxml2` installed system wide (i.e., is a there a `/usr/lib/libxml2*so` file)? What is the commandline used to build your application? If you try to use some `libxml2` functions directly from your application, can the linker solve those references? – Marco Leogrande Jun 13 '12 at 03:27
  • libxml2 is a library `$ ls -h /usr/lib/libxml2.*` returns `/usr/lib/libxml2.a /usr/lib/libxml2.so /usr/lib/libxml2.so.2.7.8 /usr/lib/libxml2.la /usr/lib/libxml2.so.2` i have used these commands to build my application `/usr/bin/g++ -I/usr/include/libxml2 -Xlinker -zmuldefs -fPIC -c neoclassic.cc` – Hasan Ali Khattak Jun 13 '12 at 08:56
  • when i used `-llibxml2` it gives error `/usr/bin/ld: cannot find -llibxml2` – Hasan Ali Khattak Jun 13 '12 at 09:01
  • when i run my main application `g++ -lneoclassic -lrt -o common/ptypes2tcl common/ptypes2tcl.o` `/usr/local/lib/libneoclassic.so: undefined reference to 'xmlXPathNewContext'` so i thought to look through my libneoclassic.so `nm -D /usr/local/lib/lneoclassic.so | grep xmlXPathNewContext` and it returned me `U xmlXPathNewContext` – Hasan Ali Khattak Jun 13 '12 at 11:04
  • When you link your application you need to add `-lxml2` to the command line; **beware** it's `-lxml2`, not `-llibxml2`. So, use `g++ -lneoclassic -lrt -lxml2 -o common/ptypes2tcl common/ptypes2tcl.o` – Marco Leogrande Jun 13 '12 at 17:56
  • i tried but was not working so i tried `g++ -o common/ptypes2tcl common/ptypes2tcl.o -lneoclassic -lrt -lxml2` and it worked like a charm. i think i need to learn about Makefiles. but thank @marco-leogrande – Hasan Ali Khattak Jun 14 '12 at 13:34
0

You may have to pass the path also using -I/path/to/library, or alternatively export it to LD_LIBRARY_PATH.

Omar Khan
  • 68
  • 6