您的位置:首页 > 其它

gcc编译器实验---对gcc编译参数的详细说明

2013-01-07 20:44 393 查看
[root@localhost gcc]# ls

hello.c

[root@localhost gcc]# gcc hello.c -o hello //“-o”,编译生成可执行文件hello(预处理、编译、汇编、连接一步完成)

[root@localhost gcc]# ls

hello hello.c

[root@localhost gcc]# ./hello

hello world!!

[root@localhost gcc]# gcc -E hello.c -o hello.i //“-E”,只进行预处理,不做其它处理,生成hello.i文件

[root@localhost gcc]# ls

hello hello.c hello.i //可以用cat hello.i查看hello.i文件内容,可以看到头文件#include <stdio.h>经过预处理后,编译器已将stdio.h内容贴近来

[root@localhost gcc]# gcc -S hello.c -o hello.s //“-S”编译生成汇编代码

[root@localhost gcc]# ls

hello hello.c hello.i hello.s

[root@localhost gcc]# gcc -C hello.c -o hello.o

gcc: GCC does not support -C or -CC without -E

[root@localhost gcc]# gcc -c hello.c -o hello.o //小“-c”,只是编译不链接,生成目标链接库“hello.o”文件

[root@localhost gcc]# ls

hello hello.c hello.i hello.o hello.s

[root@localhost gcc]# rm hello -r

rm:是否删除 一般文件 “hello”? y

[root@localhost gcc]# ls

hello.c hello.i hello.o hello.s

[root@localhost gcc]# gcc hello.o -o hello //将编译好的hello.o链接库文件,生成可执行文件hello

[root@localhost gcc]# ls

hello hello.c hello.i hello.o hello.s

[root@localhost gcc]# ./hello

hello world!!

[root@localhost gcc]#

[root@localhost gcc]# gcc -g hello.c -o hello1 //“-g”,编译生成带有标准信息的可执行文件hello1,可以使用GDB调试

[root@localhost gcc]# ls

hello hello1 hello.c hello.i hello.o hello.s

[root@localhost gcc]# ./hello1

hello world!!

[root@localhost gcc]# gcc hello.c -o static hello2

gcc: hello2:没有那个文件或目录

[root@localhost gcc]# gcc hello.c static -o hello2

gcc: static:没有那个文件或目录

[root@localhost gcc]# gcc hello.c -static -o hello2 //“-static”,静态链接库生成的可执行文件hello2

[root@localhost gcc]# ls

hello hello1 hello2 hello.c hello.i hello.o hello.s

[root@localhost gcc]# ./hello2

hello world!!

[root@localhost gcc]# ll

总计 560

-rwxr-xr-x 1 root root 4729 11-29 18:51 hello

-rwxr-xr-x 1 root root 5881 11-29 20:40 hello1

-rwxr-xr-x 1 root root 518848 11-29 20:43 hello2 //静态链接库的可执行文件hello2比动态连接库的可执行文件hello大得多

-rw-r--r-- 1 root root 74 11-29 08:31 hello.c

-rw-r--r-- 1 root root 18001 11-29 18:47 hello.i

-rw-r--r-- 1 root root 876 11-29 18:50 hello.o

-rw-r--r-- 1 root root 446 11-29 18:49 hello.s

[root@localhost gcc]# gcc -O2 hello.c -o hello3 //“-O2”,完成程序的优化工作

[root@localhost gcc]# ls

hello hello1 hello2 hello3 hello.c hello.i hello.o hello.s

[root@localhost gcc]# ./hello3

hello world!!

[root@localhost gcc]# ll

总计 568

-rwxr-xr-x 1 root root 4729 11-29 18:51 hello

-rwxr-xr-x 1 root root 5881 11-29 20:40 hello1

-rwxr-xr-x 1 root root 518848 11-29 20:43 hello2

-rwxr-xr-x 1 root root 4745 11-29 20:45 hello3 //优化的可执行文件hello3,文件大小比较

-rw-r--r-- 1 root root 74 11-29 08:31 hello.c

-rw-r--r-- 1 root root 18001 11-29 18:47 hello.i

-rw-r--r-- 1 root root 876 11-29 18:50 hello.o

-rw-r--r-- 1 root root 446 11-29 18:49 hello.s
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: