Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there restrictions on executables compiled with g++ on one Linux distro being used on another?

Tags:

c++

c

linux

gcc

g++

Are there any idiosyncrasies or variations between distros that would affect C++ binaries compiled with GCC 4.7.x on one distro being used directly on another? I understand that the ideal situation is to compile from source on the second distro but I'd really prefer not to worry about compiling new GCC versions and the program source code on my production machine. I'm a relatively inexperienced linux user (hence the question!) and still prefer IDEs as opposed to command line compilation, ssh being all I can really use to access the production machine.

The code itself is nothing interesting but it does make use of some run of the mill OS facilities like blocking sockets and the like.

Any advice would be greatly appreciated!

like image 210
Elliott Avatar asked Oct 19 '12 15:10

Elliott


3 Answers

Unless the binaries are built on exactly the same OS (including version) and exactly the same hardware there are no guarantees.

In practice:

  1. If the hardware is the same family of chip it should work.

    • This is because most people don't turn on the hardware specific optimizations (but they can).
    • Moving binaries across chip sets is highly unlikely to work
    • Moving binaries from older to newer members of a hardware family is likely to work
    • Moving binaries from newer to older members of a hardware family is less likely (but will depend on optimization and compiler settings (ir moving from 64 to 32 bit architecture is unlikely to work).
  2. If the OS has the same Major number then it should (probably) work.

    • The version of OS that a binary will work across will depend on the version of the compiler used to build it and the host OS.
    • If the compiler has changes in the ABI it generates then all bets are off. But usually a change in the ABI generated by the compiler will be a major issue and thus only happen at major points in the OS road map (not at minor increments).
  3. My advice build from source.

    • Don't specifically go out and update the development environment (use the one that comes with distribution (if you do the default updates they will not break backwards compatibility)).
    • Building is easy just read the README file. But usually it involves running two commands ./configure and make. If you don't want anything special you usually do not need to do anything else.
like image 50
Martin York Avatar answered Nov 11 '22 13:11

Martin York


G++ has had a stable ABI for quite a while, so that shouldn't cause problems. What is likely to cause problems is using dynamically linked libraries. The system running the program will need to have compatible versions of any shared libraries that the executable was compiled against. If you use only static linking, you shouldn't have a problem. You can turn on static linking by using the -static option.

like image 26
Dirk Holsopple Avatar answered Nov 11 '22 12:11

Dirk Holsopple


With static linkage, two conditions must be met:

1) The target system and the build system must be of same architecture (with exceptions: you can run 32-bit binaries on many 64-bit hosts)

2) The (g)libc package on the target system must not be an older version than on the build system (you can sometimes get away with minor version differences)

It gets more complicated with dynamic linkage.

like image 27
idefixs Avatar answered Nov 11 '22 12:11

idefixs