首页 > 解决方案 > c++ 中的预处理和搜索路径如何工作?

问题描述

我有一个 clang.C 文件:

#include "A.h"
#include "B.h"

int main(int argc, char* argv[])
{
   return 1;
}

我还有这些包含路径,它们将按顺序传递给编译器参数:

/path/to/B.h
/path/to/A.h
/different/path/to/B.h  <-- duplicate file

当我将所有 3 条路径(按顺序)添加到我的预处理器语句并运行它时,我得到了这个:

# 1 "clang.C"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "clang.C"
# 1 "/path/to/A.h" 1
# 2 "clang.C" 2
# 1 "/path/to/B.h" 1
# 3 "clang.C" 2

int main(int argc, char* argv[])
{
   return 1;
}

我希望该/path/to/B.h行应该是/differnt/path/to/B.h因为指针跳过/path/to/B.h并位于/path/to/A.h. 当它需要读取B.h时,从 开始/path/to/A.h,没有找到它,然后转到-I包含的下一条路径,即/different/path/to/B.h. 这是预处理的工作原理吗?如果没有,有人可以正确解释吗?

标签: c++c-preprocessor

解决方案


推荐阅读