如何让 GCC 在当前源文件目录之前的目录中搜索标头?
我在具有多架构构建的项目中使用 GCC 预编译头,但是当我尝试将其放置在与当前源目录不同的目录中时,事情就会崩溃。
该文件包含双引号,如果我将其更改为尖括号,它就可以工作,但问题是我有很多其他项目使用相同的预编译头名称,因此将它们全部更改为尖括号是不可取的因为它可能会导致在 Visual Studio 构建相同文件时包含哪个标头产生歧义。
所以问题是如何在不更改所有预编译标头(包括尖括号指令)和使用已弃用的 GCC 开关的情况下使其工作?
I am using GCC precompiled headers in my project with multi-architecture build, but things break down when I try to place it in a directory different from current source's directory.
The file is included with double quotes, and it works if I change it to angle brackets, but the problem is that I have a lot of other projects that use the same precompiled header name, so changing all of them to angle brackets is not desirable as it may create ambiguity about which header to include in Visual Studio build of the same files.
GCC searches current directory for double-quote includes before its search path. I can work around it using -I-
option (e.g. -Ipch_dir.i686 -I-
), so that precompiled headers directory is searched before the current directory, but this option is deprecated. GCC suggests I use -iquote
, but it does not have the same effect as -I-
.
So the question is how do I make it work without changing all precompiled headers include directives to angle brackets and using a deprecated GCC switch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个解决方法。
以不同的名称构建预编译头。例如头文件是
ah
,原始预编译头文件是pchdir.i686/ahgch
,构建为使用GCC的
-include
开关确保重命名的标头包含在其他任何内容之前(甚至在原始ah
之前),例如源文件中的最终包含顺序将是:
a-precompiled.h.gch
、ah
,我已使用检查过-H
。包含原始标头,但实际上并未处理,因为预编译标头具有相同的包含防护(也可通过在构建预编译标头后在原始标头中插入#error
进行验证)。I have found a workaround.
Build a precompiled header under a different name. For example is the header is
a.h
, original precompiled header ispchdir.i686/a.h.gch
, build it asUse GCC's
-include
switch to make sure the renamed header is included before anything else (even before the originala.h
), e.g.Final inclusion order in the source file will be:
a-precompiled.h.gch
,a.h
, which I've checked with-H
. Original header is included, but is not actually processed because precompiled header has identical include guards (verified as well by inserting an#error
in the original header after building the precompiled one).