The C++ compilation process

The C++ compilation process

Compiling a source code file in C++ is a four-step process. For example, if you have a C++ source code file named prog1.cpp and you execute the compile command

   g++ -Wall -std=c++11 -o prog1 prog1.cpp

the compilation process looks like this:

  • The C++ preprocessor copies the contents of the included header files into the source code file, generates macro code, and replaces symbolic constants defined using #define with their values.

  • The expanded source code file produced by the C++ preprocessor is compiled into the assembly language for the platform.

  • The assembler code generated by the compiler is assembled into the object code for the platform.

  • The object code file generated by the assembler is linked together with the object code files for any library functions used to produce an executable file.

By using appropriate compiler options, we can stop this process at any stage.
To stop the process after the preprocessor step, you can use the -E option:

   g++ -Wall -std=c++11 -E prog1.cpp

The expanded source code file will be printed on standard output (the screen by default); you can redirect the output to a file if you wish. Note that the expanded source code file is often incredibly large - a 20 line source code file can easily produce an expanded file of 20,000 lines or more, depending on which header files were included.

To stop the process after the compile step, you can use the -S option:

   g++ -Wall -std=c++11 -S prog1.cpp

By default, the assembler code for a source file named filename.cpp will be placed in a file named filename.s.

To stop the process after the assembly step, you can use the -c option:

   g++ -Wall -std=c++11 -c prog1.cpp

By default, the assembler code for a source file named filename.cpp will be placed in a file named filename.o.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值