Linux系统编程 24 gcc编译常用参数
指定头文件
什么时候指定头文件
当你的头文件和源码不在同一级目录下的时候
小细节:
vim file
然后在末行模式下输入
:vsp hello.c
这个时候,有两个分屏
ctrl+ww来切换
$ls
file file2 hello hello.c hello.h
$gcc hello.c -o hello
$./hello
hello.world!
The vaule of is 3.140000
The vaule of is 3.1400
(1) -I 用来指定头文件的目录位置,-I 放的位置可以比较随意
$ls
file file2 hello hello.c hello.h inc
$mv hello.h ./inc
$ls
file file2 hello hello.c inc
$gcc -I./inc hello.c -o hello11 //指定头文件的目录位置
$./hello11
hello.world!
The vaule of is 3.140000
The vaule of is 3.140000
The vaule of is 3.140000
(2)-c
预处理 编译 汇编
得到的二进制文件
(3)-g
编译时添加调试文件,文件会比不带-g较大些
主要支持gdb调试。
$ls
file file2 hello.c inc
$gcc hello.c -I./inc -o hello
$ls
file file2 hello hello.c inc
$gcc hello.c -I./inc -o hellog -g
$ls
file file2 hello hello.c hellog inc
$ll
total 52
drwxrwxrwx 3 root root 4096 Dec 7 22:19 ./
drwxrwxrwx 6 ubuntu ubuntu 4096 Dec 2 08:45 ../
-rw-rw-r-- 1 ubuntu ubuntu 0 Dec 4 19:34 file
--w-r--r-- 1 ubuntu ubuntu 0 Dec 4 19:45 file2
-rwxrwxr-x 1 ubuntu ubuntu 8571 Dec 7 22:19 hello*
-rwxrwxrwx 1 ubuntu ubuntu 724 Dec 7 22:05 hello.c*
-rw-r--r-- 1 ubuntu ubuntu 12288 Dec 7 09:18 .hello.c.swo
-rwxrwxr-x 1 ubuntu ubuntu 9571 Dec 7 22:19 hellog*
drwxrwxr-x 2 ubuntu ubuntu 4096 Dec 7 22:07 inc/
(4)gdb 调试工具对它调试
必须要加-g的生成的文件,
主要支持gdb调试。
(5)-On n=0~3 编译优化,n越大优化越多
默认是2级
灯亮灭亮灭
(6)-Wall
显示所有的警告信息
file file2 hello hello.c hellog inc
$vi hello.c
$gcc hello.c -Wall
hello.c:1:19: fatal error: hello.h: No such file or directory
#include "hello.h"
^
compilation terminated.
$gcc hello.c -Wall -I./inc
hello.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
void main ()
^
hello.c: In function ‘main’:
hello.c:5:6: warning: unused variable ‘i’ [-Wunused-variable]
int i=90;
(7)-D
程序当中去注册一个宏
file file2 hello.c inc
$gcc -I./inc hello.c -o hello -D HELLO
$./hello
hello.world!
The vaule of is 3.140000
The vaule of is 3.140000
The vaule of is 3.140000
hello.c 的代码如下:
#include "hello.h"
#ifdef HELLO
#define PI 3.14
#endif
void main ()
{
int i=90;
printf("hello.world!\n");
printf ("The vaule of is %f\n ",PI );
printf ("The vaule of is %f\n ",PI );
printf ("The vaule of is %f\n ",PI );
}