通用的运行代码可以看如下博文:​​cpp运行通用过程(易理解)​​

先看看Linux下对于g++的开发文档吧

man gcc

DESCRIPTION
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The “overall options” allow you to stop
this process at an intermediate stage. For example, the -c option says not to run the linker. Then the output consists of object
files output by the assembler.

翻译如下:当你调用GNU的时候,他正常情况下会进行预处理,编译,汇编,和链接,所有的过程你都可以在中间过程中停止这个进程,例如,-c选择不能进行链接,这个输出由编译的二进制文件组成

可知此处应该分成四个过程

  • 预处理:条件编译,宏变量和文件引用,生成*.i
  • 编译:进行词法分析和语法分析,生成汇编指令,生成*.s
  • 汇编:把汇编代码翻译成机器语言的过程,生成*.o
  • 链接:链接静态链接库.a(可重定向目标文件)和动态共享库.so(共享的目标文件),生成*.out可执行文件

下面我们来看看具体的文件运行过程吧

预处理
(此处文件引入,条件编译和宏变量转换代码量过多,不做概述)

编译:词法分析和语法分析,生成汇编代码

testlh@testlh-virtual-machine:~$ cat demo.s
.file "demo.cpp"
.text
.section .rodata
.type _ZStL19piecewise_construct, @object
.size _ZStL19piecewise_construct, 1
_ZStL19piecewise_construct:
.zero 1
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.text
.globl main
.type main, @function
main:
.LFB1522:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movq %fs:40, %rax
movq %rax, -8(%rbp)
xorl %eax, %eax
movl $0, -20(%rbp)
movl $1, -16(%rbp)
movl $2, -12(%rbp)
leaq -20(%rbp), %rax
movq %rax, %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZNSolsEPKv@PLT
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT
leaq -16(%rbp), %rax
movq %rax, %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZNSolsEPKv@PLT
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT
leaq -12(%rbp), %rax
movq %rax, %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZNSolsEPKv@PLT
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT
movl $0, %eax
movq -8(%rbp), %rcx
xorq %fs:40, %rcx
je .L3
call __stack_chk_fail@PLT
.L3:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1522:
.size main, .-main
.type _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB2009:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
cmpl $1, -4(%rbp)
jne .L6
cmpl $65535, -8(%rbp)
jne .L6
leaq _ZStL8__ioinit(%rip), %rdi
call _ZNSt8ios_base4InitC1Ev@PLT
leaq __dso_handle(%rip), %rdx
leaq _ZStL8__ioinit(%rip), %rsi
movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rax
movq %rax, %rdi
call __cxa_atexit@PLT
.L6:
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2009:
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
.type _GLOBAL__sub_I_main, @function
_GLOBAL__sub_I_main:
.LFB2010:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $65535, %esi
movl $1, %edi
call _Z41__static_initialization_and_destruction_0ii
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2010:
.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I_main
.hidden __dso_handle
.ident "GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0"
.section .note.GNU-stack,"",@progbits
.section .note.gnu.property,"a"
.align 8
.long 1f - 0f
.long 4f - 1f
.long 5
0:
.string "GNU"
1:
.align 8
.long 0xc0000002
.long 3f - 2f
2:
.long 0x3
3:
.align 8
4:

汇编:把汇编代码翻译成机器语言的过程

testlh@testlh-virtual-machine:~$ nm -C demo.o
U __cxa_atexit
U __dso_handle
U _GLOBAL_OFFSET_TABLE_
0000000000000110 t _GLOBAL__sub_I_main
0000000000000000 T main
U __stack_chk_fail
00000000000000c3 t __static_initialization_and_destruction_0(int, int)
U std::ostream::operator<<(std::ostream& (*)(std::ostream&))
U std::ostream::operator<<(void const*)
U std::ios_base::Init::Init()
U std::ios_base::Init::~Init()
U std::cout
U std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
0000000000000000 r std::piecewise_construct
0000000000000000 b std::__ioinit

链接:不做概述