gcc编译选项-I与搜寻路径(下)

一 点睛

本实战验证双引号包含头文件时的搜索次序。

二 实战

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值