How to use static library in ROOT program

I have a program that uses pugixml library:

#include "pugixml.h"

When I use it in c++ I simply load it with

g++ MyProgram.cpp -l pugixml

How to use it when this library is supposed to be used in ROOT program?
Note that this is not working:

gSystem->Load("/usr/local/lib/libpugixml.a");
.L MyProgram.cpp+

the above causes errors:

/home/wolfgang/BINP/Diploma/Analysis/omega_pi0/Algorithm/XML_Parser_cpp_ACLiC_dict.o: In function GetCuts(float)': XML_Parser_cpp_ACLiC_dict.cxx:(.text+0x217): undefined reference topugi::xml_document::xml_document()’

How to load static library in ROOT?

Try:

gSystem->AddLinkedLibs("-lpugixml");
.L MyProgram.cpp++

or:

gSystem->AddLinkedLibs("-L/usr/local/lib -lpugixml");
.L MyProgram.cpp++

or:

gSystem->AddLinkedLibs("/usr/local/lib/libpugixml.a");
.L MyProgram.cpp++

In all 3 cases I got the following output:

Info in TUnixSystem::ACLiC: creating shared library /home/wolfgang/BINP/Diploma/Analysis/omega_pi0/Algorithm/./XML_Parser_cpp.so
/usr/bin/ld: /usr/local/lib/libpugixml.a(pugixml.cpp.o): relocation R_X86_64_32 against `.rodata’ can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libpugixml.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
Error in : Compilation failed!

You need to recompile “libpugixml” adding “-fPIC” (even though it is an archive library), if you want to use it in dynamically linked shared objects.
Otherwise you will need to build your own standalone executables (linking them to your “libpugixml.a” as is).

I am afraid static libraries can’t be dynamically loaded.
That’s just the way it is.
For a library to be dlopen’ed (what is happening when you load it from the ROOT prompt) it need to be compiled in a special way.
A .a library isn’t a .so library.
Recompile it to make it a shared library as indicated by the error message.

hth,
-s

Thank you all so much. Finally, I have made it work.


For those people who do not know much about cmake, make, ... (like me) I provide a solution that I found how to recompile library with -fPIC option (assuming using of cmake):
Just add these lines to your CMakeLists.txt:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" )
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC" 

And then run

cmake path_to_CMakeLists
make
make install

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.