Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to compile a C/C++ source code that executes in all Linux distributions without recompilation?

Tags:

c++

c

linux

Is it possible to compile a C/C++ source code that executes in all Linux distributions without recompilation?

If the answer is yes, can I use any external (non-standard C/C++) libraries?

I want distribute my binary application instead of distribute of source code.

like image 370
Amir Saniyan Avatar asked Sep 18 '11 11:09

Amir Saniyan


People also ask

How do I compile CPP in terminal?

cpp file that you want to compile, follow the following instructions to compile and run it. Step 1 − Open a new terminal window or cmd if you are on windows. Step 3 − Now enter the following command to compile the source file using g++. In place of <name-you-want-to-give> replace it by any name like myprogram, etc.


1 Answers

No, you can't compile an executable the executes in all Linux distributions. However, you can compile an executable that works on most distributions that people will tend to care about.

  1. Compile 32-bit. Compile for the minimum CPU level you're willing to support.

  2. Build your own version of glibc. Use the --enable-kernel option to set the minimum kernel version you're willing to support.

  3. Compile all other libraries you plan to use yourself. Use the headers from your glibc build and your chosen CPU/compiler flags.

  4. Link statically.

  5. For anything you couldn't link to statically (for example, if you need access to the system's default name resolution or you need PAM), you have to design your own helper process and API. Release the source to the helper process and let them (or your installer) compile it.

  6. Test thoroughly on all the platforms you need to support.

You may need to tweak some libraries if they call functions that cannot work with this mechanism. That includes dlopen, gethostbyname, iconv_open, and so on. (These kinds of functions fundamentally rely on dynamic linking. See step 5 above. You will get a warning when you link for these.)

Also, time zones tend to break if you're not careful because your code may not understand the system's zone format or zone file locations. (You will get no warning for these. It just won't work.)

Most people who do this are building with the minimum supported CPU being a Pentium 4 and the minimum supported kernel version being 2.6.0.

like image 56
David Schwartz Avatar answered Oct 12 '22 22:10

David Schwartz