C++预编译、编译、汇编、链接详解

一、准备阶段

  • g++编译环境,配置好环境变量

  • 新建一个helloworld.cpp
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello, world!" << endl;
    return 0;
}
  • 命令查看

     g++ --help

二、预编译

     预处理阶段对自定义和预定义的宏进行展开,把包含的头文件内容插入到当前源文件,并根据预处理指令包含不同的代码,或者设置设置传递给编译器的参数。
    使用gcc的-E和-v选项来查看整个预处理过程。-E选项表示只对源文件做预处理,-v选项表示显示详细的预处理过程,-o选项表示将预处理后的结果输出到helloworld.i文件中

文件 helloworld.i 部分

# 1 "helloworld.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "helloworld.cpp"
# 1 "D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/iostream" 1 3
# 36 "D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/iostream" 3
       
# 37 "D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/iostream" 3

# 1 "D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/c++config.h" 1 3
# 186 "D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/c++config.h" 3
namespace std
{
  typedef long long unsigned int size_t;
  typedef long long int ptrdiff_t;

三、编译

     编译阶段由编译器ccl把预处理后的文件内容转换成汇编程序。我们可以使用gcc的-S选项来实现编译的过程。-S选项便是只做编译处理

.file	"helloworld.cpp"
.lcomm _ZStL8__ioinit,1,1
	.def	__main;	.scl	2;	.type	32;	.endef
	.section .rdata,"dr"
.LC0:
	.ascii "Hello, world!\0"
	.text
	.globl	main
	.def	main;	.scl	2;	.type	32;	.endef
	.seh_proc	main
main:
.LFB1048:
	pushq	%rbp
	.seh_pushreg	%rbp
	movq	%rsp, %rbp
	.seh_setframe	%rbp, 0
	subq	$32, %rsp
	.seh_stackalloc	32
	.seh_endprologue
	call	__main
	leaq	.LC0(%rip), %rdx
	movq	.refptr._ZSt4cout(%rip), %rcx
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	movq	.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(%rip), %rdx
	movq	%rax, %rcx
	call	_ZNSolsEPFRSoS_E
	movl	$0, %eax
	addq	$32, %rsp
	popq	%rbp
	ret
	.seh_endproc
	.def	__tcf_0;	.scl	3;	.type	32;	.endef
	.seh_proc	__tcf_0
__tcf_0:

四、汇编

 汇编器as会把helloworld.s的内容翻译成机器指令,这些机器指令按照ELF(Executable and Linking Format)被打包成重定位的二进制目标文件。使用-c选项来实现目标文件的生成,使用type命令用于查看目标文件的信息。

五、链接

  helloworld.c中引用了标准C库中的printf函数,所以在最后的链接阶段,ld链接器会把printf函数符号位于libc.so.6库中的重定位和符号表信息复制到最终的可执行文件helloworld 中。gcc链接时库的搜索路径在使用-v选项时会显示出来

六、运行

链接阶段生成helloworld.exe,直接运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值