在C条件的#include [英] Conditional #include in C

查看:191
本文介绍了在C条件的#include的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,使一个条件包括与C preprocessor?

Is there a way to make a conditional include with the c preprocessor?

我有一个工具(Tools.c,的Tools.h),由不同的团队共享图书馆。这个库依赖于第二个,提供XML的功能。让我们称之为一个XML.h现在一队使用了第二个库(XML.h)的普通版本,而另一个团队使用在他们的项目的扩展版本(XMLEx.h)。二队不希望包括XML.h,因为他们已经XMLEx.h包括,提供XML.h的所有功能。

I have a "library" of tools (Tools.c, Tools.h), shared by different teams. This library depends on a second one, providing XML-capabilities. Lets call that one XML.h Now one team uses a plain version of the second library (XML.h), while another team uses an extended version (XMLEx.h) in their project. The second team doesn't want to include the XML.h because they already have XMLEx.h included, providing all functions of XML.h.

有实施类似的机制:

#ifdef XML_EX
#include "XMLEx.h"
#else
#include "XML.h"
#endif

仅在的#define XML_EX 上一个更高的(项目)的水平?像

only with the #define XML_EX on a higher (project) level? Like

#include "Tools.h"

有关团队1

#define XML_EX
#include "Tools.h"

有关二队? (我知道,简单的解决办法是行不通的,它更说明所需的工作流程。)

for Team 2? (I know that simple solution would not work, it's more to illustrate the desired "workflow".)

边界条件是:


  • Windows系统的

  • CVI / LawWindows IDE

  • 无作出文件

  • 的Tools.h应该编译没有外部的项目。

编辑:
对于建议的解决方案,并暗示,它是常见的方式,也许它是与我的构建过程,但它不工作。 IDE将在抱怨缺少XML.h团队2.我想,这是因为IDE首先尝试编译每个源独立的,不知道对外的#define。

For the suggested solution and the hint, that it is the common way, maybe it has something to do with my build process, but it doesn't work. The IDE is complaining about missing XML.h for team 2. I guess, it's because the IDE tries to compile each source standalone first, not knowing about the "outer" #define.

编辑:
让我们pretend,A队是一群白痴怎么可能只打在IDE中的运行按钮。因此,最好有工作开箱。

Let's pretend, team A is bunch of morons how could only hit the "RUN" button in the IDE. Therefore it best has to work out of the box.

编辑:
好吧,我一直困惑不已。所有我已经学会告诉我,它应该工作。下面是一组最小的,我已经测试:

Okay, it puzzled me. All I've learned tells me, it should work. Here is a minimal set, I've tested:

的main.c

#define XML_EX
#include "Tools.h"

void main(void)
{
  test();
}

然后的Tools.h

then Tools.h

#ifdef XML_EX
  #include "XMLEx.h"
#else
  #include "XML.h"
#endif

XMLEx.h有测试功能UND XMLEx.c实现它。如果我尝试建立,我得到一个XML.h未找到。一切都建立精细与的#define XML_EX 中的Tools.h。

推荐答案

您可以有一个定义了A队任何宏,也不定义他们队B或定义不同的值team.h文件和每个小组将有自己team.h的副本。

You could have a team.h file that defines any macros for Team A and doesn't define them for Team B or defines different values and each team would have their own copy of team.h .

每个团队成员也可以传递价值的编译器通过-D选项宏(至少在Linux编译器支持)。我建议,如果你这样做,你通过TEAM_A或TEAM_B为了给你更多的灵活性,在未来,而不是必须通过更多的宏。

Each team member could also pass that value to the compiler via the -D option a macro (at least the Linux compilers support that). I would recommend that if you do that you pass TEAM_A or TEAM_B in order to give you more flexibility in the future and not have to pass more macros.

这篇关于在C条件的#include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆