我如何基于编译器指令-D自动选择一个include.h文件?

问题描述 投票:1回答:2

我想在我的程序中有一个常规的include.h头文件,该文件将列出使用-D ITEM = ITEM1编译器标志进行选择时要使用的所有可能包含文件。例如,如果我要为ITEM1构建此库,则对item2使用-D ITEM = ITEM1,它将为-D ITEM = ITEM2。

header.h文件:

#ifdef ITEM1
#include item1.h
#endif
#ifdef ITEM2
#include item2.h
#endif

头文件item1.h和item2.h将是工作目录外部另一个目录中的符号链接

[执行此方案时,我在item1.h或item2.h中的每个定义上都得到范围错误

c++ include header-files preprocessor compiler-flags
2个回答
3
投票

您可以使用-D ITEM1-D ITEM2来发布代码。如果要基于-D ITEM=...的逻辑,则需要使用-D ITEM=1-D ITEM=2并将代码更改为:

#ifdef ITEM

#if ITEM == 1
#include item1.h
#elif ITEM == 2
#include item2.h
#else
// Unknown value of ITEM. Figure out what do for this case.
#endif

#else
// ITEM is not defined. Figure out what do for this case.
#endif

2
投票

您使用的-D错误。当你做

-D ITEM=ITEM1

您定义ITEM符号,并为其赋予值ITEM1。这不是您想要的,因为它未定义ITEM1ITEM2,因此都不包含任何文件。您需要的是

-D ITEM1
//or
-D ITEM2

以定义这些符号中的任何一个。

© www.soinside.com 2019 - 2024. All rights reserved.