我正在使用 IAR 开发一个项目。
在项目中,有多个位置可以搜索包含文件。
有两个位置都具有相同名称的文件。
因此,在 C 文件中我们有 #include "some_file.h"
编译器/预处理器如何处理这个问题?它是否包含它找到的第一个 some_file.h
?
我的猜测...它按照 IAR 项目定义中定义的顺序在目录中进行搜索。然后它会在第一个匹配处停止。这是正确的吗?
最佳答案
通常使用 #include "foo.h"
时首先相对于当前源文件搜索文件,而 #include <foo.h>
会更喜欢标准路径。
GCC looks for headers requested with #include "file" first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat.h contains #include "types.h", GCC looks for types.h first in /usr/include/sys, then in its usual search path.
补充:
通常,自定义路径也会按照它们在命令行中出现的顺序进行搜索。
所以对于-I/opt/include -I$dependency/include
和#include "foo.h"
将搜索以下路径:
- ./foo.h(与包含文件相同的目录)
- /opt/include/foo.h(第一个自定义路径)
- $dependency/include/foo.h(第二个自定义路径...)
- /usr/include/foo.h(标准搜索路径)
您还可以通过查看 gcc -E foo.c
的输出来准确找出包含哪些文件。 .
参见here如何-I
和-iquote
工作。
关于来自多个位置的 C 包含文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28944628/