shenyan
shenyan

Reputation: 428

Linux, compile a piece of code in one host, to run in another?

The code is written in c/c++,may depend some libs in the compiling host; and it should run in another host without libs depending problems. Both hosts are linux, may have different versions. Do you have a good strategy?

Upvotes: 2

Views: 428

Answers (4)

linuxbuild
linuxbuild

Reputation: 16133

Take all answers for this question into account (static linking, compiling on the oldest Linux, etc.) and then check your final binary by the Linux App Checker to show compatibility issues with other Linux distributions.

enter image description here

Upvotes: 0

Gearoid Murphy
Gearoid Murphy

Reputation: 12126

Pure static linking on Linux is discouraged, it's only really possible if you use an alternative libc (such as dietlibc) which isn't an option with C++, my favoured approach is to build the application on the oldest version of Linux you have to support as the newer libc builds will have backwards compatibility.

This will only cover libc, other requirements, such as gtk, pangom, etc will have to be compiled directly into your binary.

Upvotes: 3

arash kordi
arash kordi

Reputation: 2558

Most platforms have a well-defined ABI that covers C code, but ABIs that cover C++ functionality are not yet common.

A program written in c++ using just libc may not run on another platform. if binary compatibility is an important issue consider using c.

Upvotes: 0

unwind
unwind

Reputation: 400039

Link the application statically, so that it depends on as few dynamically loaded libraries as possible. This is the general solution to this problem.

Other solutions include:

  • Ship the required libraries with the application, and override the system's LD_LIBRARY_PATH variable to make the included versions the preferred ones.
  • Write code to dynamically load the libraries using dlopen() and friends, and handle differences in library versions manually.

Upvotes: 2

Related Questions