如何在gcc中排除当前路径下的标题

时间:2016-03-21 09:55:50

标签: c++ gcc

假设我有两个目录producttest

./test
./test/test_product.cpp
./test/foo.h
./product
./product/product.cpp
./product/product.h
./product/foo.h

product.cpp包括foo.h

#include "foo.h"

默认行为是包含/product/foo.h。 但是,我想在/test/foo.h中包含/product/product.cpp,而不对产品目录进行任何修改。 有没有办法做到这一点?非常感谢!

4 个答案:

答案 0 :(得分:2)

您可以添加GCC预处理器用于查找带有HttpServer选项的头文件的搜索路径,然后在包含文件时可以使用尖括号,然后预处理器应查找相同的文件目录作为源文件的最后一个。

换句话说,添加例如-I-Itest目录添加到包含文件搜索路径。然后使用test代替。如果您想使用#include <foo.h>,只需删除product/foo.h选项并添加-Itest即可。

还有另一种方法,涉及条件编译solution suggested by Lanting

你可能有例如。

-Iproduct

然后使用#ifdef TEST # include "../test/foo.h" #else # include "foo.h" #endif 进行构建,您定义了宏test/foo.h,可以使用TEST选项在命令行上完成:

-D

要使用$ gcc -DTEST ... ,请删除该选项,以便不再定义product/foo.h

答案 1 :(得分:0)

您可以在包含中添加标题的完整和相对路径。

#include "../test/foo.h"
例如,

会工作

查看有关如何以及何时使用哪些包含表单的讨论:What are the benefits of a relative path such as "../include/header.h" for a header?How to use #include directive correctly?

答案 2 :(得分:0)

您可以通过在#include下提供其路径来在文件中包含标头。  恩。  #include&#34; .h文件的路径&#34;

答案 3 :(得分:0)

假设两个文件都有不同的标题保护,则可以使用标题保护将其存档, 您可以在make文件的标志列表中添加-D(不需要的文件的标题保护)。

相关问题