Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C++ into portable linux binaries

Ok, this question is about portable as in no dependencies (i.e. "I can put the binaries in a USB key and bring it with me everywhere, e-mail them to friends etc").

I have heard of static linking but I'm confused to what are exactly the consequences of it; what can and what can't be static linked (i.e. what about Qt, OpenGL, libstdc++?) and to which degree the binary will be "portable" afterwards.

I've also heard of LSB (Linux Standard Base) but I don't know exactly what it is or if it can help in this sense.

like image 818
Giovanni Funchal Avatar asked Apr 06 '11 09:04

Giovanni Funchal


1 Answers

You don't have to link all of the libraries the same way. I'd definitely stick with dynamic linking for libc and the other system libraries. And use static linking for anything C++; the binary API does change from time to time, and you need to be sure that the version of the library is the same as the version you compiled against---the surest way of doing that is to statically link the library into your executable. If any of the other libraries you use are written in C++, you'll probably want to compile them locally as well, rather than using a precompiled distribution, to ensure that they are compiled against the same binary API, and link them statically. The binary API for C is fixed, so you have more freedom: if the library is going to be present on every installation, and must have a version compatible with the version of the OS, link dynamically; otherwise, statically.

like image 136
James Kanze Avatar answered Oct 19 '22 09:10

James Kanze