从c/c++源文件,到能够执行文件,须要如下几个步骤:c++
下面咱们以hello world程序为例,展现整个编译连接过程。编程
1. 编写hello.c代码ubuntu
#include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; }
2.使用gcc –E hello.c –o hello.i, 将源文件hello.c文件预处理生成hello.iide
3.编译, gcc –S hello.i –o hello.s, 生成汇编程序hello.s,对于x86系统,生成x86汇编代码。spa
ction .rodata .LC0: .string "Hello World!" .text .globl main .type main, @function main: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 leaq .LC0(%rip), %rdi call puts@PLT movl $0, %eax popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size main, .-main .ident "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0" .section .note.GNU-stack,"",@progbits
4.汇编 gcc –c hello.s –o hello.o, 生成目标机器码。3d
5.连接,和系统库文件进行连接,ld hello.o –o hello, 执行会出错,只靠一个hello.o不能生成一个完整的可执行文件。code
gcc hello.c –o hello 能够直接生成可执行文件。blog