gcc编译选项-include和-Wall

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/88382467

一 选项-include

1 点睛

gcc命令行中也能包含头文件。很多开源软件都是这样,有的时候开源软件源码中找不到“#include <xxx.h>”这样的代码,而xxx.h的内容又确实被引用了。这就是-include搞的鬼。在gcc编译时通过-include来保护xxx.h。

2 使用方式

gcc [srcfile] -include [headfile]

3 实战

1 目录结构

[root@localhost 2.14]# vi inc/test.h
[root@localhost 2.14]# tree
.
├── inc
│   └── test.h
└── test
    └── test.cpp

2 test.h内容

#define ZWW 9

3 test.cpp内容

#include <stdio.h>
// 注意:本文件没有包含test.h
int main()
{
    bool b = false;
    printf("hello, boy:%d\n",ZWW);
    return 0;
}

4 运行结果

[root@localhost test]# gcc test.cpp -include /root/C++/ch02/2.14/inc/test.h -o test
[root@localhost test]# ./test
hello, boy:9

二 选项-Wall

1 点睛

选项-Wall显示所有警告信息。Warn all,显示所有警告。

2 代码

#include <stdio.h>
int main()
{
        bool b = false;  //b没有使用
        int i;
        printf("hello, boy:%d\n",i); //i没有赋值就开使用
        return 0;
}

3 使用选项-Wall进行编译,编译出告警,但也能编译成功

[root@localhost test]# gcc test.cpp -Wall -o test
test.cpp: In function ‘int main()’:
test.cpp:5:14: warning: unused variable ‘b’ [-Wunused-variable]
         bool b = false;
              ^
test.cpp:7:36: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
         printf("hello, boy:%d\n",i); 
                                    ^
[root@localhost test]# ll
total 16
-rwxr-xr-x. 1 root root 8520 Mar 10 19:28 test
-rw-r--r--. 1 root root  161 Mar 10 08:07 test.cpp
[root@localhost test]# ./test
hello, boy:0

4 不使用选项-Wall进行编译和运行

[root@localhost test]# gcc test.cpp -o test
[root@localhost test]# ./test
hello, boy:0

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/88382467
今日推荐