C + + compilation process

Source: Internet
Author: User

Basic process of compiling
graph TD    A(预处理) --> B(编译)    B --> C(汇编)    C --> D(链接)

Detailed procedures
#include <stdio.h>int main(intchar *argv[]){  printf("Hello World!\n");  return0;}
Pretreatment (CPP)

gcc -E hello.c -o hello.i

preprocessing, mainly dealing with the following directives: macro definition directives, conditional compilation directives, header files contain directives. Preprocessing is basically an "alternative" work for the source program. In this substitution, a file is generated that has no macro definition, no conditional compilation directives, and the header files are expanded (recursively expanded).

Compiling (CCL)

gcc -S hello.i -o hello.s

Compiling, is to "translate" C + + code into assembly code.

Assembly (AS)

gcc -c hello.s -o hello.o

Assembly, which translates the generated assembly code into a machine code that conforms to a certain format, is generally shown as an elf target file on Linux.

Link (LD)

gcc hello.o -o hello

Links to the generated target files and system library files, resulting in executable files that can be run on a specific platform. Why do you want to link some target files (crt1.o, CRTI.O, etc.) in the system library? These target files are used to initialize or recycle the C runtime environment, such as the initialization of the heap memory allocation context, in fact, the CRT is the C runtime abbreviation. This implies another point: The program is not executed from the main function, but starts from a portal in the CRT, which is _start on Linux. And by default, the LD is to add these system library files (itself is a dynamic library) are dynamically linked to the application, if you want to do in a static connection, you need to display the parameters of the specified LD command-static.

Other

In addition, there is an optimization phase. Optimization is a very difficult technology in the compilation system. It concerns not only the compiler technology itself, but also the hardware environment of the machine has a great relationship. Optimization is part of the optimization of the intermediate code. This optimization is not dependent on the specific computer. The other optimization is mainly for the generation of target code. For the former optimization, the main work is to delete the common expressions, loop optimization (out-of-code, strength weakening, transformation loop control conditions, known amount of consolidation, etc.), replication propagation, and deletion of useless assignments, and so on. The latter type of optimization is closely related to the hardware structure of the machine, and the most important consideration is how to make full use of the values of the variables stored in each hardware register of the machine to reduce the number of accesses to memory. In addition, how to carry out instructions according to the characteristics of the machine hardware (such as pipelining, RISC, CISC, VLIW, etc.) and some of the instructions to make the target code is relatively short, the efficiency of execution is high.

Reference
    1. C + + program compilation process (pre-compilation, compile-so links)
    2. C + + Compile link procedure

C + + compilation process

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.