简体   繁体   English

如何让GCC在当前源文件目录之前的目录中搜索头文件?

[英]How to make GCC search for headers in a directory before the current source file's directory?

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. 我在我的项目中使用GCC预编译头与多架构构建,但当我尝试将它放在与当前源目录不同的目录中时,事情就会崩溃。

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. 该文件包含双引号,如果我将其更改为尖括号,它可以工作,但问题是我有很多其他项目使用相同的预编译头名称,因此将所有这些更改为尖括号是不可取的因为它可能会产生关于在Visual Studio构建相同文件中包含哪个头的歧义。

GCC searches current directory for double-quote includes before its search path . GCC在搜索路径之前搜索当前目录中的双引号包含 I can work around it using -I- option (eg -Ipch_dir.i686 -I- ), so that precompiled headers directory is searched before the current directory, but this option is deprecated. 我可以使用-I-选项(例如-Ipch_dir.i686 -I- )解决它,以便在当前目录之前搜索预编译的头目录,但不推荐使用此选项。 GCC suggests I use -iquote , but it does not have the same effect as -I- . GCC建议我使用-iquote ,但它与-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? 所以问题是如何在不改变所有预编译头文件包括尖括号的指令和使用弃用的GCC开关的情况下使其工作?

I have found a workaround. 我找到了一个解决方法。

  1. Build a precompiled header under a different name. 使用其他名称构建预编译头。 For example is the header is ah , original precompiled header is pchdir.i686/ahgch , build it as 例如标题是ah ,原始预编译头是pchdir.i686/ahgch ,将其构建为

     gcc ah -o pchdir.i686/a-precompiled.h.gch 
  2. Use GCC's -include switch to make sure the renamed header is included before anything else (even before the original ah ), eg 使用GCC的-include开关确保重命名的标头包含其他任何东西之前(甚至在原始的之前ah ),例如

     gcc -Ipchdir.i686 -include a-precompiled.h <other arguments> <source> 
  3. Final inclusion order in the source file will be: a-precompiled.h.gch , ah , which I've checked with -H . 源文件中的最终包含顺序是: a-precompiled.h.gchah ,我用-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). 包含原始标头,但实际上并未处理,因为预编译标头具有相同的包含保护(通过在构建预编译的标头之后在原始标头中插入#error来验证)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM