views:

625

answers:

6

Hi All,

What would the purpose of this construct in a c file be?:

#define _TIMERC  
#include "timer.h"  
#undef _TIMERC

I am aware of the guard for preventing multiple inclusion of a header file. This doesn't appear to be whats happening though.

thanks!

+2  A: 

I was wondering if it could be this:

The header file in this case is intended to allow multiple inclusions with different defines established before the each #include.

If in the timer.h there is a block of code (interrupt code) for timers A, B and C for each timer in the microcontroller. In some cases timer A is required in one module and timer C is required in another module.

JeffV
+1  A: 

I think your self-answer is right. There is most likely conditional stuff in the included header and the "calling" file knows which specific set of conditional "stuff" it wants to include.

It does not necessarily have to do with multiple includes - it can just be special cases depending on the "calling" context.

I am not exactly sure why one would undefine though. I can't think of a case where I would mix and match so not sure why an undefine is necessary.

Tim
A: 

At the risk of stating the obvious, "timer.h" expects to have _TIMERC and the rest of your code does not.

Clearly not good practice in the general case, but I have seen similar when including third party code. Can get nasty when you have #defs that clash...

Stephen Darlington
+4  A: 

Often times a library header file will have multiple options, that are enabled and disabled by macro defines. This will enable such an option.

More typically these are set at a global scope by configuring your build system to add (for eg with gcc) -D_TIMERC to the compilers command line.

Greg Rogers
+4  A: 

Here's a scenario to illustrate...

Lets say that timer.h provides a macro tick_count() that returns the number of timer interrupts that occured.

One module (rpm_reader.h) using timer A for interval timing:

#define _TIMERA   
#include "timer.h"   
#undef _TIMERA

In another module (lap_time.h) is using timer C for its interval timing

#define _TIMERC  
#include "timer.h"  
#undef _TIMERC

The rpm_reader would return the tick count from timer A when it called tick_count() and lap_time would get its count from timer C.

(My apologies for answering my own question, but asking the question helped me come to this revelation.)

JeffV
This would be a very unclear implementation of a header and prone to error.I have worked on systems where this approach was used so that default values could be placed with the variables in the header file, using a macro.This could then be called like#define __INIT_VARS__#include "file.h"
itj
A: 

For the record, common practice to avoid multiple includes of the same header file is to put the guard in the file itself, not to rely on some external define... ^_^

The headers start with:

#ifndef header_name_h
#define header_name_h

and end with:

#endif

Of course, the def style can vary.

Thus, on first inclusion, we go past the #ifndef (not yet defined) and we set the macro. On second inclusion, if any, we just jump to end of file, nothing is included.

PhiLho
True, but in this case I think the intention is to include multiple times.
JeffV