External static libraries

Is it possible to use external static libraries in ROOT?

In particular, I was trying to use absl::StrCat from the abseil-cpp library in ROOT (interactively). Abseil comes with a CMakeLists.txt that produce libabsl_strings.a. Can I somehow load this into ROOT?

I managed to do it by adding
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
to the CMakeLists.txt, recompiling, running

ar -x libabsl_strings.a
g++ -std=c++11 -shared *.o -o libabsl_strings.so

And then loading the .so file like this:

root [0] R__LOAD_LIBRARY(/path/build/abseil-cpp/absl/strings/libabsl_strings.so)
root [1] #include <absl/strings/str_cat.h>
root [2] string s = absl::StrCat("Hallo", 4, ":")
(std::string &) "Hallo4:"

But this is probably not how it should be done… It seemed rather complicated and unpacking + repacking .a to .so feels wrong. Can’t one use the .a file directly? Do I need PIC? When I compile programs normally, I can simply link the .a file.

How do I do this “right”? I’m not an expert with all the build tools.

Assuming that your library was compiled with “-fPIC” (even though it is an archive library), you can try:

Ah, this works for scripts:

 gSystem->AddLinkedLibs("/path/libabsl_strings.a");
.x script.c+

But it doesn’t work interactively on command line before running the .x or .L command (with +). Hm… is it a good idea to put something like gROOT->ProcessLine(".L lib_loader.C+"); in the rootlogon.C file?

That’s what I was doing myself (first “AddLinkedLibs” and then loading an almost “dummy.cxx++” script which used anything from my library, so that the linker was “forced” to take it, but you may also need to play with “-Wl,-whole-archive” or its equivalent on OSX).
But I think a better solution is to re-link your archive library into a shared one (like you do it in your first post above). I guess you could try to simplify this step: g++ -std=c++11 -shared -o libabsl_strings.so -Wl,-whole-archive libabsl_strings.a

I tried the “-Wl,-whole-archive” in my CMakeLists yesterday (I wanted to produce the .so file from there) but somehow failed with some linker errors (maybe I should try from command line fist to find out if it is a cmake issue or a “real” problem). Thanks a lot for your suggestion with the AddLinkedLibs. It works for now. I will try with that switch again as soon as I find some time for it.

Note that in your makefile, you should have something like -Wl,-whole-archive libabsl_strings.a -Wl,-no-whole-archive (and maybe there should be two -- charactes in “-Wl,--...”, I’m not sure now).

yes, read that on stackoverflow and didn’t know how to do it with CMake :cry:

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