How does a source program become an executable binary program?
The following describes how a program under linux x86 becomes an executable binary file. The following program exchanges the values of two array elements.
The source code of main. c and the source code of swap. c are as follows:
1 #include<stdio.h>2 #define SIZE 23 int buf[SIZE]={1,2};4 void swap();5 int main(){6 printf("before swap buf[0]=%d ,buf[1]=%d\n",buf[0],buf[1]); 7 swap();8 printf("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; }
Using the following command, main. c and swap. c generate an executable binary file swap
Gcc mian. c swap. c-o swap
So does this command convert the source program into executable programs?
The procedure is as follows:
1. The driver first calls the C Preprocessor (CPP) to translate the source file into an ASCII intermediate file mian. i. The pre-processor inserts all the content contained in # include into the declared position, performs macro replacement, and performs main. replace the SIZE of row 3rd of the c file with 2 (note that this is just a simple text replacement ).
2. Next, the C compiler of the driver translates main. I into the main. s program of the assembly language.
3. the assembler of the driver then translates the main. s assembly language program into a relocated binary file main. o.
4. Run the linker to set main. o and swap. o and some necessary system target files (for example, the printf function you will call is printf. o) connect to an executable binary file.