How does a source program become an executable binary program?

Source: Internet
Author: User

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.

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.