Skip to content
Advertisement

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

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement