6

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.

5
  • 2
    Have you tried it? That's exactly how lots of system headers work.
    – Kerrek SB
    Sep 7, 2012 at 8:34
  • 1
    Check this I think this article may be what you are looking for or a start at least... Sep 7, 2012 at 8:36
  • @Kerrek: Yes. I expected it to work, but for some reason my IDE build button won't work, complaining about missing XML.h for Team 2.
    – Oliver
    Sep 7, 2012 at 8:37
  • @Oliver: you'll have to work out what that reason is, because there's nothing wrong with the code posted here. Maybe there's another file somewhere in the project that includes "Tools.h" without first defining XML_EX, and that is used by Team 2 -- that's a good reason for putting the define in the project settings instead of in the source file. Sep 7, 2012 at 8:43
  • Well. It seems I missed a very importend step in my build process. Restart the god damned IDE. -.- :D Thank's everyone for ensuring me, that my first idea was actualy right and working.
    – Oliver
    Sep 7, 2012 at 10:06

3 Answers 3

3

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.

0

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.

2
  • Let's pretend, team A is bunch of morons how could only hit the "RUN" button in the IDE.
    – Oliver
    Sep 7, 2012 at 8:45
  • Done. I forced in a -D macro through the project file. After all it is the best solution and while no one even notice this "funny things in that file", nobody has to worry.
    – Oliver
    Sep 10, 2012 at 8:58
0

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.