一 点睛
本实战验证双引号包含头文件时的搜索次序。
二 实战
1 建立4个test1.h文件
[root@localhost test]# find / -name test1.h
/root/C++/ch02/2.11/test/test1.h
/root/C++/ch02/2.11/inc/test1.h
/usr/include/test1.h
/usr/local/include/test1.h
2 4个文件的内容
[root@localhost test]# cat /root/C++/ch02/2.11/test/test1.h
#define ZWW 11
[root@localhost test]# cat /root/C++/ch02/2.11/inc/test1.h
#define ZWW 8
[root@localhost test]# cat /usr/include/test1.h
#define ZWW 10
[root@localhost test]# cat /usr/local/include/test1.h
#define ZWW 9
3 /root/C++/ch02/2.11/testtest.cpp源文件内容
#include <stdio.h>
#include "test1.h"
int main()
{
bool b = false;
printf("hello, boy:%d\n",ZWW);
return 0;
}
4 测试
4.1 4个.h文件都存在的情况下,执行下面的命令,匹配到了/root/C++/ch02/2.11/test/test1.h
[root@localhost test]# gcc test.cpp -I ../inc -o test
[root@localhost test]# ./test
hello, boy:11
4.2 删除/root/C++/ch02/2.11/test/test1.h,匹配到/root/C++/ch02/2.11/inc/test1.h
[root@localhost test]# gcc test.cpp -I ../inc -o test
[root@localhost test]# ./test
hello, boy:8
4.3 删除/root/C++/ch02/2.11/inc/test1.h,匹配到/usr/local/include/test1.h
[root@localhost test]# gcc test.cpp -I ../inc -o test
[root@localhost test]# ./test
hello, boy:9
4.4 删除/usr/local/include/test1.h,匹配到/usr/include/test1.h
[root@localhost test]# gcc test.cpp -I ../inc -o test
[root@localhost test]# ./test
hello, boy:10