生成中间文件
当前目录下有一hello.c
//hello.c
#include<stdio.h>
int main()
{
printf("hello world!!!");
return 0;
}
获得预处理以后的文件 hello.i
gcc -E hello.c //只执行预编译,即处理所有#开头的部分 如:
#include<stdio.h>
#define x 5
//p.s. -E只会输出在控制台上,如果想要得到hello.i,用:
gcc -E hello.c > hello.i
# 1 "hello.c"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "hello.c"
# 1 "/usr/include/stdio.h" 1 3 4
/*
...省略一万行...
*/
extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
# 943 "/usr/include/stdio.h" 3 4
# 2 "hello.c" 2
int main()
{
printf("hello world!!!");
return 0;
}
获得汇编程序 hello.s
gcc -S hello.s //直接获得hello.s(里面都是汇编指令 用编译器打开即可看到)
"hello.s"
.file "hello.c"
.section .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
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
.section .note.GNU-stack,"",@progbits
获得可重定位目标文件 hello.o
gcc -c hello.c //二进制代码,做连接器试调可以用,普通人无法读懂
获得可执行目标程序 a.out
gcc hello.c //a.out 可以直接运行
版本
c大致可以分为两个常用版本 C90和C99
其中C90和ANSI C是一样的
用C90标准编译
gcc -std=c89 hello.c //或者
gcc -ansi hello.c
用C99标准编译
gcc -std=c99 hello.c
P.S.
gcc -O1 -o exe hello.c //-O1:一级编译优化(同理-O2就是二级优化) 等级越高得出汇编代码效率越高
//-o name : 输出程序的名字 而不再叫a.out