简体   繁体   English

有没有办法告诉g ++编译器,而不是在某个-I路径中寻找include标头?

[英]Is there a way to tell the g++ compiler, not to look for include header in a certain -I path?

I'm trying to compile a cpp file that uses include headers from 2 folder locations. 我正在尝试编译一个cpp文件,该文件使用来自2个文件夹位置的包含标头。 Both the folders has lot of headers that are necessary for my file. 这两个文件夹都有许多我的文件必需的标题。

Now, one of the header file is present in both the folders, but the problem is they are of different version. 现在,两个文件夹中都存在一个头文件,但是问题是它们的版本不同。 Hence the functions in that common header have same name but different API signature. 因此,该公共标头中的函数具有相同的名称,但具有不同的API签名。

Something like this: 像这样:

Folder A: 资料夹A:

  • foo.hpp foo.hpp
  • bar1.hpp bar1.hpp
  • bar2.hpp bar2.hpp
  • bar3.hpp bar3.hpp

Folder B: 文件夹B:

  • foo.hpp foo.hpp
  • bar4.hpp bar4.hpp
  • bar5.hpp bar5.hpp
  • bar6.hpp bar6.hpp

API of function foobar from foo.hpp of folder A: void foobar(arg1, arg2); 文件夹A的foo.hpp中的函数foobar的API: void foobar(arg1, arg2);

API of function foobar from foo.hpp of folder B: void foobar(arg1, arg2, arg3); 文件夹B的foo.hpp中的函数foobar的API: void foobar(arg1, arg2, arg3);

#include "foo.hpp"
#include "bar1.hpp"
...
#include "bar4.hpp"
...
...

int main(){
...
foobar (arg1, arg2);
...
}

g++ main.cpp -o MyExe -I< path-to-folder-A > -I< path-to-folder-B >

This throws the errors like multiple redefinition of function , no matching function call etc., for various functions in the header. 对于标头中的各种功能,这会引发错误,例如多次重定义功能没有匹配的功能调用等。

So, my question is: Are there any flags to tell the compiler only to consider the definition found from folder A and ignore the one from folder B? 因此,我的问题是:是否有任何标志告诉编译器仅考虑从文件夹A找到的定义而忽略从文件夹B找到的定义?

Note on code limitations : I cannot alter the folders or files of A and B in any manner. 关于代码限制的注意事项 :我不能以任何方式更改A和B的文件夹或文件。 Neither can I give absolute paths to the headers instead of -I. 我也不能给标头提供绝对路径,而不是-I。

Assuming that you can give more elaborate relative paths: 假设您可以给出更详尽的相对路径:

If folder b is a subfolder of main.cpp's, you can just use 如果文件夹b是main.cpp的子文件夹,则可以使用

#include "<relative-path-to-folder-b>/foo.hpp"

If folder b isn't a subfolder and you can add another -I directive, then add another -I directive: 如果文件夹b不是子文件夹,则可以添加另一个-I指令,然后添加另一个-I指令:

g++ main.cpp -o MyExe -I<path-to-folder-before-b> -I< path-to-folder-A > -I< path-to-folder-B >

and then add the include 然后添加包含

#include "b/foo.hpp"

One (very brittle, hack-ish and possibly slow-to-compile) way of solving this relies on the manual include guards ( #ifndef MY_HEADER etc.) that are presumably present in the headers in question: 解决此问题的一种方法(非常脆弱,容易破解,并且可能编译缓慢)依赖于手册中的防护措施( #ifndef MY_HEADER等),这些防护#ifndef MY_HEADER可能存在于相关标头中:

  1. Gather all the header files that you want to use (excluding all the ones you don't want/need). 收集所有要使用的头文件(不包括所有不需要的头文件)。

  2. Create a central include file that #include s all of these files (by as absolute of a path as you can, ie make sure this picks the correct foo.hpp gathered above, not one of the "original" ones). 创建一个中央包括文件#include一切都将这些文件(由绝对的路径就可以了,即确保此拾取正确的foo.hpp聚集上面,而不是“原始”的人之一)。

  3. Tell your compiler to force-include this central include file. 告诉编译器强制包含此中央包含文件。 Realistically, you should just make this a precompiled header (or include it in yours). 实际上,您应该将其设置为预编译的标头(或将其包含在您的标头中)。

  4. Due to force-including all these headers at the start of every translation unit, all the include guard defines are already set before you ever reach the point of the "wrong" headers being included. 由于在每个转换单元的开头都强制包含所有这些标头,因此在您到达要包含的“错误”标头之前,已经设置了所有包含保护定义。 The compiler will still copy-paste them in there, but the include guards will prevent any code in them from being considered. 编译器仍将它们复制粘贴到该位置,但是包含保护将阻止考虑其中的任何代码。

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

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