Pages

What is cross compilation

The desktops and laptops that we use in our daily lives are mostly of x86 architecture. Thus when we compile a program using the usual compilers like gcc,turbo c etc the executable generated is compatible with x86 only and this can not be used to execute the same program on any other architecture.
For example consider this simple program

hello.c :


Now let us compile this using gcc



This generates an executable hello. To find out more details about this executable run the command "file" on the executable.



The output of the "file" command clearly indicates that "hello" is a 32 bit executable for intel 80386.
If we try to execute the same executable on another architecture like arm, it will not execute and will throw an error.

Other architectures like arm are generally in a very restricted environments where programming is not as easy as it is on PCs.
Thus to aid in programming for other architectures on PCs itself, special compilers exist and when a code is compiled using these special compilers, the executable generated will be for the architecture for which the compiler is supposed to generate for.
For example there is a compiler for arm from the GNU ,"gnuarm", available at http://gnuarm.com, which can be used to compile c programs on x86 machines(PCs) and generate executable for arm.
This process of generating executable for one architecture while compiling the code on another architecture is termed as cross compilation.
Compile the above program using the gnuarm compiler

Now use the command "file" on the executable



We can see that the executable is for 32 bit arm, and if we try to execute this on our PCs, It does not execute and throws an error as shown below.



No comments:

Post a Comment