Four steps for GCC to compile source code

Source: Internet
Author: User

There are four steps for GCC to compile C source code: preprocessing ----> compiling ----> compilation ----> Link.

You can use GCC parameters to control the execution process, so that you can gain a deeper understanding of the process of compiling C Programs.

The following describes how to compile a program.

#include <stdio.h>int main(){    printf("happy new year!\n");    return 0;}

  

 

1: preprocessing:The compiler compiles the header file of the C program and replaces the Macro. You can refer to the gcc parameter-E.

Command: gcc-E hello. c-o hello. I

Purpose: Output hello. c preprocessing hello. I

At this stage, the compiler mainly performs lexical analysis, syntax analysis, semantic analysis, and so on. After checking that there are no errors, the code is translated into an assembly language. Refer to the gcc parameter-S.

The compiler (ccl) translates the text file hello. I into the text file hello. s, which contains an assembly language program. Each statement in an assembly language program is described in a standard text format.

A low-level machine language command.

Command: gcc-S hello. I-o hello. s
Purpose: Compile the pre-processing output file hello. I into a hello. s file.

3: Assembly:Convert the. s file generated in the compilation phase to the binary target code. Refer to gcc parameter-c. The assembler (as) translates hello. s into machine language commands, packs these commands into formats that can be relocated to the target program, and saves the results in the target file hello. o. The hello. o file is a binary file whose Byte encoding is the machine language.

Command: gcc-c hello. s-o hello. o

Purpose: Compile and output the Assembly output file hello. s to output the hello. o file.

4: link:Link the obj file to an executable file: The linker (ld) is responsible for merging the. o file. The result is the hello file, which is the target file of the course execution. It can be loaded into the memory and called by the system.

Command: gcc hello. o-o hello

Related Article

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.