How the source program file becomes an executable binary file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.