Conditional #include in C

2019-03-30 08:50发布

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

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.

Is there a mechanism to implement something like:

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

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

#include "Tools.h"

for Team 1 and

#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".)

Boundary conditions are:

  • Windows system
  • CVI/LawWindows IDE
  • no make files
  • Tools.h should compile without an outer project.

EDIT: 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.

EDIT: 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.

EDIT: 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();
}

then Tools.h

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

XMLEx.h has the test function und XMLEx.c implements it. If I try to build, I get an "XML.h not found". Everything builds fine with a #define XML_EX in the Tools.h.

3条回答
够拽才男人
2楼-- · 2019-03-30 08:55

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 .

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.

查看更多
来,给爷笑一个
3楼-- · 2019-03-30 09:03

I fooled myself. As stated above, there is nothing wrong with the code. I missinterpreted the errormessage of my IDE.

As to stay with the example, I had also the Tools.c and the compilation of that source failed (naturally), because I hadn't XML.h in my project. Removing the source from the projekt and just link against the precompiled Tools works fine.

查看更多
The star\"
4楼-- · 2019-03-30 09:11

The solution you suggested is usually the one being used, and it is done pretty often. You can also pass the macro in the makefile / project file.

Also note, that Tools.h is a header file, and will always be compiled as part of another file.

查看更多
登录 后发表回答