Pre-processor directives problem

Hi,

I'm trying to use this code to prevent including the same header file more than once:
1
2
3
#ifndef PHIDGET_INTERFACE_KIT_H
#include "PhidgetInterfaceKit.h"
#endif 

You can see that I want to include PhidgetInterfaceKit.h.
But it doens't seem to work, what do I do wrong?
1) That only works if PhidgetInterfaceKit.h #defines PHIDGET_INTERFACE_KIT_H. The #ifndef looks for that symbol being defined -- it doesn't really look to see whether or not a header has been included already or not.

2) Don't put your #includes in guards like that. Instead, put the entire header in a guard:

1
2
3
4
5
6
7
// in PhidgetInterfaceKit.h:
#ifndef PHIDGET_INTERFACE_KIT_H
#define PHIDGET_INTERFACE_KIT_H

// -- put the actual header stuff here

#endif 


1
2
// in whatever.cpp
#include "PhidgetInterfaceKit.h"  // no need to guard this, the header is already guarded 



EDIT:

also, obligatory link: http://cplusplus.com/forum/articles/10627/

See sections 3 and up
Last edited on
Wow, thanks for the link and help/info!
Topic archived. No new replies allowed.