gcc编译流程有:预处理、编译、汇编、链接
每个过程分别产生相应的中间文件。
预处理:.i
编译:.s
汇编:.o
连接:.exe
下面以一个例子说明,仅main.h和main.c两个文件。
main.h 内容
char str[] = "hello";
main.c 内容
#include<stdio.h>
#include"main.h"
int main()
{
printf("Hello! /n");
printf("%s/n",str);
getchar();
return 0;
}
命令:gcc -E main.c
查看预处理结果,-E选项将把预处理的结果,写入stdout。
也可以将与处理结果输出到文件中:gcc -E main.c -o output.txt
命令:gcc -E main.c -O main.i
输出预处理结果main.i
命令:gcc -save-temps -S main.c
1个中间文件: main.i(预处理结果)
1个输出文件:main.s(汇编代码)
注意:使用-save-temps保留中间文件
其中预处理结果main.i为:
# 1 "main.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "main.c"
# 1 "C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 1 3
# 19 "C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 3
# 1 "C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/_mingw.h" 1 3
# 31 "C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/_mingw.h" 3
# 32 "C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/_mingw.h" 3
# 20 "C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h" 2 3
.....省略部分
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetwchar (void);
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputwchar (wint_t);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getw (FILE*);
int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putw (int, FILE*);
# 2 "main.c" 2
# 1 "main.h" 1
char str[] = "hello";
# 3 "main.c" 2
int main()
{
printf("Hello! This is our embedded world!/n");
printf("%s/n",str);
getchar();
return 0;
}
编译结果main.s为:
.file "main.c"
.globl _str
.data
_str:
.ascii "hello/0"
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.align 4
LC0:
.ascii "Hello! This is our embedded world!/12/0"
LC1:
.ascii "%s/12/0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
call __alloca
call ___main
movl $LC0, (%esp)
call _printf
movl $_str, 4(%esp)
movl $LC1, (%esp)
call _printf
call _getchar
movl $0, %eax
leave
ret
.def _getchar; .scl 2; .type 32; .endef
.def _printf; .scl 2; .type 32; .endef