【问题标题】:Is there a way to ask gcc to treat #include <> like #include ""?有没有办法让 gcc 将 #include <> 视为 #include ""?
【发布时间】:2015-02-08 01:07:11
【问题描述】:

是否有编译器或预处理器标志会强制 gcc 像对待 #include "x.h" 一样对待 #include &lt;x.h&gt;?我有一堆生成的代码使用#include &lt;&gt; 处理当前目录中的文件,gcc 报告这些文件没有此类文件或目录。我正在寻找一种不涉及编辑代码的解决方法。

编辑:-I. 不这样做。假设我有以下文件:

foo/foo.h:

#include <foo2.h>

foo/foo2.h:

#define FOO 12345

xyz/xyz.c

#include <stdio.h>
#include "foo/foo2.h"

int main(void)
{
    printf("FOO is %d\n", FOO);
    return 0;
}

如果在xyz目录中,我用gcc -o xyz I.. xyz.c编译,编译失败:

In file included from xyz.c:2:
../foo/foo.h:1:18: error: foo2.h: No such file or directory
xyz.c: In function ‘main’:
xyz.c:6: error: ‘FOO’ undeclared (first use in this function)
xyz.c:6: error: (Each undeclared identifier is reported only once
xyz.c:6: error: for each function it appears in.)

添加-I. 不会改变任何东西。

但是,如果我将 foo/foo.h 更改为:

#include "foo2.h"

然后编译工作。我知道我可以将-I../foo 添加到我的命令行中,但我正在寻找一种更通用的方法来将#include &lt;&gt; 视为#include ""。有吗?

【问题讨论】:

    标签: c gcc header include c-preprocessor


    【解决方案1】:

    是的,您可以将开关-I . 传递给编译器以将当前目录添加到包含搜索路径中。

    【讨论】:

      【解决方案2】:

      -I- 选项可能会对您有所帮助。来自 gcc 的手册页:

         -I- Split the include path.  Any directories specified with -I options
             before -I- are searched only for headers requested with
             "#include "file""; they are not searched for "#include <file>".  If
             additional directories are specified with -I options after the -I-,
             those directories are searched for all #include directives.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-15
        • 2021-02-18
        • 2018-06-23
        • 2023-04-03
        • 2021-06-17
        • 1970-01-01
        • 2012-10-30
        • 2015-06-05
        相关资源
        最近更新 更多