Compiling C++ into portable linux binaries

前端 未结 4 1997
清酒与你
清酒与你 2021-02-14 02:02

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 t

相关标签:
4条回答
  • 2021-02-14 02:45

    To disappoint you: there is no solution to it. That statically link is there, and you can (if you want) link everything static => all dependencies from other libraries are removed. But there are other dependencies, which cant be avoided: First there is the architecture. We have here linux on PowerPC, linux on ARM, linux on Microblaze, linux on 32-bit x86, and linux on 64-bit x86. Secondly there is the the ABI and there syscalls. Those can (and indeed have in the past) change (e.g. exotic/new syscalls does not exist on old systems - and if you got those calls in your binary your program wont work).

    The LSB is just a a standard (or better it tries to be - not everyone follows it) for the different distros to make adminstration, usage, and maintaining (and sometimes developing) easier by e.g. defining where which files are stored. It does not aim to make executables more portable.

    0 讨论(0)
  • 2021-02-14 02:54

    Be careful with static linking using gcc, it does not really work any more. See

    0 讨论(0)
  • 2021-02-14 02:59

    Static linking works for most libraries, but not those that use dynamically loaded modules. Just try and see if it works. You may still have issues with kernel compatibility; your program may be using system calls not available in older kernels.

    The Linux Standard Base is supported by some Linux distros, but on Debian (and I think also Ubuntu) it has to be installed from a package. It also handles mostly administrative things like startup scripts, though it has some binary compatibility stuff. See this page for some info.

    For the "put on USB key and run anywhere" requirement, check out CDE.

    0 讨论(0)
  • 2021-02-14 03:01

    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.

    0 讨论(0)
提交回复
热议问题