3

I'm writing a small program in C and I want to document it so others could change some details. I wrote a config.h file with #defines that enable some operation or not, some of them are mutually exclusive, others aren't.

Example of not mutually exclusive:

#define USE_A
#define USE_B
#define USE_C

Example of mutually exclusive:

#define BUILD_FULL
#define BUILD_COMPACT

Until now I selected the configuration of the program to compile by documenting out the features I don't want to use, for example:

//#define BUILD_FULL
#define BUILD_COMPACT

to build a COMPACT executable.

How can I write documentation for the commented out features?

I tried:

#define USE_A //!< Special Feature
#define USE_B //!< Special Feature
//#define USE_C //!< Special Feature

I compiled it without USE_C but obviously I lost the documentation because doxygen didn't parse it. How would you rewrite the #define flow if I misunderstand the right way of using defines?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
andyinno
  • 1,021
  • 2
  • 9
  • 24

1 Answers1

3

You can test for the value of the macro, instead of just its presence. For example, use 0 to disable an option, or 1 to enable it. Then, use #if instead of #ifdef to test the condition.

#define USE_A 1    //!< Special Feature
#define USE_B 1    //!< Special Feature
#define USE_C 0    //!< Special Feature

#if USE_A
...
#elif USE_C
...
#endif  
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • Yes I already figure that in this way I can leave the define in the proper place and so the code is visible everywhere. The only problem is that I need to edit all my code :( Time to monkey patching :) – andyinno Apr 24 '12 at 08:16