Group Abstract Group Abstract

Message Boards Message Boards

2
|
4.3K Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Creating portable LibraryLink binaries for Linux

Posted 10 years ago

I am looking for advice on how to create portable LibraryLink binaries for Linux. I would like the same binary to work on many different Linux distributions, so the users of my package don't have to compile it from scratch. But unfortunately my knowledge about the issues involved (dynamic linking on Linux, compatibility between gcc versions, etc.) is quite limited.

  • How should I compile the library to make sure it is as portable as possible?
  • What should I check for to test for portability if I only have access to a single Linux computer? (Other than using ldd to check for dependencies.)

My library is written in C++11 (which may be a problem) and I compiled it on Ubuntu 15.10 with gcc 5.2.1. Reportedly it does not work on Ubuntu 14.04, and I am not sure why. ldd lists only these dependencies:

$ ldd IGraphM.so 
    linux-vdso.so.1 =>  (0x00007fffecdf0000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f8e7a398000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8e79fce000)
    /lib64/ld-linux-x86-64.so.2 (0x000056390dc55000)

Other than these, there are two libraries statically linked on the command line, igraph and gmp. I do this by using the "ExtraObjectFiles" -> {"$HOME/local/lib/libigraph.a", "$HOME/local/lib/libgmp.a"} in CreateLibrary. This just appends the two .a files to the end of the gcc command line.

I compiled both igraph and gmp myself on the same machine.

Any hints, comments, pointers, etc. would be most welcome!


More specific questions:

  • Can it cause problems that I am linking to my own gmp version given that the Mathematica kernel already links to its own gmp?
  • Are there specific requirements on the gcc versions that can be used with LibraryLink and Mathematica 10.x? Does the answer change for C++11 or C++98 vs just plain C?
  • ldd didn't list libstdc++. How can I tell which libstdc++ is being used? Is it the one that ships with Mathematica (since the kernel process already loads it)? If yes, how does that impact on compatibility with the version of my compiler and standard library?

Finally, here's the command line that CreateLibrary constructs:

/usr/bin/gcc -shared -o\
>   "/home/szhorvat/Repos/IGraphM/IGraphM/LibraryResources/Linux-x86-64/Working\
>   -ubuntu-vb-2054-821005440-1/IGraphM.so" -m64 -fPIC -O2 -std=c++11 \
>   -I"/usr/local/Wolfram/Mathematica/10.3/SystemFiles/IncludeFiles/C"\
>   -I"/usr/local/Wolfram/Mathematica/10.3/SystemFiles/Links/MathLink/Developer\
>   Kit/Linux-x86-64/CompilerAdditions" -I"$HOME/local/include"\
>   -I"/home/szhorvat/Repos/IGraphM/IGraphM/LTemplate/IncludeFiles"\
>   "/home/szhorvat/Repos/IGraphM/IGraphM/LibraryResources/Source/LTemplate-IGr\
>   aphM.cpp"\
>   "/home/szhorvat/Repos/IGraphM/IGraphM/LibraryResources/Source/IGlobal.cpp"\
>   "$HOME/local/lib/libigraph.a" "$HOME/local/lib/libgmp.a"\
>   -L"/usr/local/Wolfram/Mathematica/10.3/SystemFiles/Links/MathLink/Developer\
>   Kit/Linux-x86-64/CompilerAdditions"\
>   -L"/usr/local/Wolfram/Mathematica/10.3/SystemFiles/Libraries/Linux-x86-64"\ 
>   -L"$HOME/local/lib"    2>&1
POSTED BY: Szabolcs Horvát
2 Replies

Thanks for the response Ilian!

Is Mathematica really shipping a libstdc++? I'd be surprised.

No, it is not. This was my misunderstanding.

To match the compiler used for building the kernel, use gcc 4.8.

Earlier today I tried building with gcc 4.8.5 on Ubuntu 15.10, and people tell me that the result does work on Ubuntu 14.04! :-) I am also going to try -D_GLIBCXX_USE_CXX11_ABI=0

POSTED BY: Szabolcs Horvát

Can it cause problems that I am linking to my own gmp version given that the Mathematica kernel already links to its own gmp?

In theory yes, though it is a remote possibility. Compiling with -Bsymbolic may be a good idea, so that the lookup scope of symbols in IGraphM.so is placed ahead of the global scope, thus ensuring the correct gmp functions will be called.

Are there specific requirements on the gcc versions that can be used with LibraryLink and Mathematica 10.x? Does the answer change for C++11 or C++98 vs just plain C?

I am not completely sure. Plain C is unlikely to cause problems, with C++ there could be subtle runtime library incompatibilities. To match the compiler used for building the kernel, use gcc 4.8. With gcc 5.1 and later on Ubuntu, you may want add the -D_GLIBCXX_USE_CXX11_ABI=0 definition.

How can I tell which libstdc++ is being used? Is it the one that ships with Mathematica

Is Mathematica really shipping a libstdc++? I'd be surprised.

POSTED BY: Ilian Gachevski
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard