Below is the Linux x86 program to explain how a source program becomes an executable binary file. The following is a simple procedure for me to illustrate the two elements in a swap array
The code for the MAIN.C source file, and the SWAP.C source file code are as follows:
1#include <stdio.h>2 #defineSIZE 23 intbuf[size]={1,2};4 voidswap ();5 intMain () {6printf"before swap buf[0]=%d, buf[1]=%d\n", buf[0],buf[1]); 7 swap ();8printf"After swap buf[0]=%d, buf[1]=%d\n", buf[0],buf[1]); 9}
extern int buf[]; void swap () { int temp; Temp=buf[0]; buf[0]=buf[1] buf[1]=temp; }
By following this command, an executable binary file swap is created
"GCC mian.c swap.c-o swap
So how does this command turn the source program into an executable program?
The steps are as follows:
1, the driver first call the C-preprocessor (CPP) to translate the source file into an ASCII intermediate file mian.i, the preprocessor will include the content contained in the location of the declaration, and do a macro substitution, the main.c file of the 3rd line Replace the size with 2 (note that this is just a simple text replacement).
2, the next driver C compiler will main.i translated into assembly language program MAIN.S
3. The assembler of the driver then translates the assembly language program main.s into a relocatable binary file main.o
4. Finally run the linker to connect MAIN.O with SWAP.O and some of the necessary system target files (such as the printf function you will invoke as PRINTF.O) to become an executable binary file.
How the source program file becomes an executable binary file