Conditional #include in C

Is there a way to do conditional inclusion with c preprocessor?

I have a "library" of tools (Tools.c, Tools.h) that are used by different teams. This library depends on the second, providing XML capabilities. Let's call that one XML.h Now one team uses the simple version of the second library (XML.h), and the other team uses the extended version (XMLEx.h) in its project. The second command does not want to include XML.h, because they already contain XMLEx.h, providing all the functions of XML.h.

Is there a mechanism for implementing something like:

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

only with #define XML_EX at a higher (project) level? how

 #include "Tools.h" 

for team 1 and

 #define XML_EX #include "Tools.h" 

for team 2? (I know that a simple solution will not work, it more illustrates the desired "workflow.")

Border conditions:

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

EDIT: For the proposed solution and a hint that this is the usual way, maybe it has something to do with my build process, but it doesn't work. The IDE complains about the lack of XML.h for command 2. I think this is because the IDE tries to compile each source separately first, not knowing the "external" #define.

EDIT: Let it pretend, Team A is a bunch of morons as soon as you hit the "RUN" button in the IDE. Therefore, it is best to work out of the box.

EDIT: Okay, that puzzled me. All I found out tells me this should work. Here is the minimal set I 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 a testing function, and XMLEx.c implements it. If I try to build, I get "XML.h not found". Everything builds fine with #define XML_EX in Tools.h.

+4
source share
3 answers

You can have a team.h file that defines any macros for team A and does not define them for team B or defines different values, and each team will have its own team.h.

Each team member can also pass this value to the compiler using the -D parameter with a macro (at least this supports Linux compilers). I would recommend that if you do, you will pass TEAM_A or TEAM_B to give you more flexibility in the future and not pass more macros.

0
source

The solution that you proposed is usually used, and it is executed quite 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.

+3
source

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

As with the example, I also had Tools.c, and compiling this source did not work (naturally) because I did not have XML.h in my project. Removing a source from a project and simply linking to precompiled tools works great.

0
source

Source: https://habr.com/ru/post/1432898/


All Articles