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

Asked
Active3 hr before
Viewed126 times

7 Answers

withoutcompilepossible
90%

Is it possible to compile a C/C++ source code that executes in all Linux distributions without recompilation?, Is it possible to write a MMA version of LineInt like Maple? ,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.,I want distribute my binary application instead of distribute of source code.

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

--enable - kernel
load more v
88%

Is it possible to compile a C/C++ source code that executes in all Linux distributions without recompilation?,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.,If the answer is yes, can I use any external (non-standard C/C++) libraries?,Is the source of ps and other process tools. They do indeed use proc (indicating it is probably the conventional and best way). Their source is quite readable. The file

Is the source of ps and other process tools. They do indeed use proc (indicating it is probably the conventional and best way). Their source is quite readable. The file

/procps-3.2.8/proc / readproc.c
load more v
72%

To link it all together into an executable file, we need to use the C++ linker (g++), since at least one file that we’ll link, print.o, was compiled in C++:,It exports main and imports C linkage printSumFloat and printSumInt, and both mangled versions of printSum.,What are those ifdef/endif conditional compilation blocks? If I include this header from a C source file, I want it to become:,If we include print.hpp from a C++ source file, the __cplusplus preprocessor macro will be defined and the file will be seen as:

The examples were compiled in a CentOS Linux machine:

$ uname - sr
Linux 3.10 .0 - 327.36 .3.el7.x86_64
load more v
65%

Source code written in the C or C++ language, present as plain text files. , Linux application executable files do not use any file name extension. Shared object (library) executable files use the .so file name extension. , Executable code, created by linking object code with a linker. Linux application executable files do not use any file name extension. Shared object (library) executable files use the .so file name extension. , Change to the directory containing the source code file(s).

The files typically use extensions such as .c, .cc, .cpp, .h, .hpp, .i, .inc. For a complete list of supported extensions and their interpretation, see the gcc manual pages:

$ man gcc
load more v
75%

The -o hello in the command is important--it tells C what to name the executable. The -lm part tells C to link in the math libraries. If your program doesn't use any of the match functions from ``math.h'', you can leave this part out. , Is there any way that you can compile this program from Emacs? Try compiling ``main.c'' this way. What happens? , Link the ``.o'' file to produce an executable with a command such as gcc -o hello hello.o -lm The -o hello in the command is important--it tells C what to name the executable. The -lm part tells C to link in the math libraries. If your program doesn't use any of the match functions from ``math.h'', you can leave this part out. , Now do it yourself. Compile, link, and run the program.

gcc - Wall - g - c hello.c

gcc - o hello hello.o - lm

gcc - Wall - g - c hello.c

gcc - o hello hello.o - lm

gcc - Wall - g - o hello hello.c - lm

int sum(int n);
int product(int n);

#include "help.h"

#include "help.h"

#include "file.h"

#include "file.h"
load more v
40%

Compile the above program in Linux by these two different commands, Default 64-bit compilation,  ,How to compile 32-bit program on 64-bit gcc in C and C++,Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.,After that you will be able to compile a 32-bit binary on a 64-bit system.How to check whether a program is compiled with 32-bit after adding a “-m32” flag? Well we can easily check this by the following program.  

Mostly compiler(gcc or clang) of C and C++, nowadays come with default 64-bit version. Well it would be a good option in terms of speed purposes. But it would lead to problem, if someone wants to run their program as a 32-bit rather than 64-bit for testing or debugging purposes. Therefore we must have a knowledge about this.
Before proceeding forward, let’s confirm which bit-version of gcc is currently installed in our system. 
Just type the following command on Linux terminal. 
 

Command: gcc - v
Output
Using built - in specs.
COLLECT_GCC = gcc
COLLECT_LTO_WRAPPER = /usr/lib / gcc / x86_64 - linux - gnu / 5 / lto - wrapper
Target: x86_64 - linux - gnu
   ......................
   ......................
Command: gcc - m32 geek.c - o geek

If you get an error as follows: 
 

fatal error: bits / predefs.h: No such file or directory

For C language:
   sudo apt - get install gcc - multilib
For C++language:
   sudo apt - get install g++ - multilib

Compile the above program in Linux by these two different commands, 
Default 64-bit compilation, 
 

Input: gcc - m64 geek.c - o out
Output: . / out
Size = 8

Forced 32-bit compilation, 
 

Input: gcc - m32 geek.c - o out
Output: . / out
Size = 4
load more v
22%

Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program. ,With -O, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time. ,If combined with -fprofile-arcs, this option instructs the compiler to add code to gather information about values of expressions. ,Reduce compilation time and make debugging produce the expected results. This is the default.

-fauto - inc - dec -
   fbranch - count - reg -
   fcombine - stack - adjustments -
   fcompare - elim -
   fcprop - registers -
   fdce -
   fdefer - pop -
   fdelayed - branch -
   fdse -
   fforward - propagate -
   fguess - branch - probability -
   fif - conversion -
   fif - conversion2 -
   finline - functions - called - once -
   fipa - modref -
   fipa - profile -
   fipa - pure -
   const -fipa - reference -
   fipa - reference - addressable -
   fmerge - constants -
   fmove - loop - invariants -
   fmove - loop - stores -
   fomit - frame - pointer -
   freorder - blocks -
   fshrink - wrap -
   fshrink - wrap - separate -
   fsplit - wide - types -
   fssa - backprop -
   fssa - phiopt -
   ftree - bit - ccp -
   ftree - ccp -
   ftree - ch -
   ftree - coalesce - vars -
   ftree - copy - prop -
   ftree - dce -
   ftree - dominator - opts -
   ftree - dse -
   ftree - forwprop -
   ftree - fre -
   ftree - phiprop -
   ftree - pta -
   ftree - scev - cprop -
   ftree - sink -
   ftree - slsr -
   ftree - sra -
   ftree - ter -
   funit - at - a - time
load more v

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