来自多个位置的 C 包含文件

标签 c include

我正在使用 IAR 开发一个项目。 在项目中,有多个位置可以搜索包含文件。 有两个位置都具有相同名称的文件。 因此,在 C 文件中我们有 #include "some_file.h" 编译器/预处理器如何处理这个问题?它是否包含它找到的第一个 some_file.h

我的猜测...它按照 IAR 项目定义中定义的顺序在目录中进行搜索。然后它会在第一个匹配处停止。这是正确的吗?

最佳答案

通常使用 #include "foo.h" 时首先相对于当前源文件搜索文件,而 #include <foo.h>会更喜欢标准路径。

参见GCC documentation :

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"将搜索以下路径:

  1. ./foo.h(与包含文件相同的目录)
  2. /opt/include/foo.h(第一个自定义路径)
  3. $dependency/include/foo.h(第二个自定义路径...)
  4. /usr/include/foo.h(标准搜索路径)

您还可以通过查看 gcc -E foo.c 的输出来准确找出包含哪些文件。 .

参见here如何-I-iquote工作。

关于来自多个位置的 C 包含文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28944628/

相关文章:

可以延迟分配静态内存吗?

c++ - 如何执行和写入标准输入并将标准输出通过管道传输到套接字?

c - C语言中输入数值时如何将光标移动到鼠标位置?

c - int 到填充零字符串

c - 您如何设置 eclipse 以便它像对 Java 一样对 C 的错误进行解析和加下划线?

Java Javadoc 包含 Private

c++ - 如何在 C++ Qt Creator 中使用库

c - 自动发现 C 依赖项

header - 处理 biicode 中 Unresolved 平台相关依赖项

ruby-on-rails-3.1 - 在 rails 3.1 中的 Assets 管道之外包含一个 JS 文件?