Include Guards, explain please?

Status
Not open for further replies.
Hi, I keep running across the following two lines at the top of header files:

#ifndef NAME
#define NAME

So these stop me from including this header file in multiple places? What if I have two separate files that both need to include these headers?


The problem I'm having in my code is that I include some header files, but when I call functions from them they don't execute as I would expect. If I use system() to call a program using the same header functions from within the not working file, things work.

So I am wondering what exactly these #ifndef/#define statements do. I read the wikipedia page already, but I need more help.
 

jbode

Ars Legatus Legionis
11,752
Subscriptor
The include guards protect against the header file from being processed more than once for the same translation unit.

The problem crops up when you include several headers that all include the same header, such as:

Code:
/* header.h */
#ifndef HEADER
#define HEADER
...
#endif

/* header1.h */
#ifndef HEADER1
#define HEADER1
#include "header.h"
...
#endif

/* header2.h */
#ifndef HEADER2
#define HEADER2
#include "header.h"
...
#endif

/* source.c */
#include "header.h"
#include "header1.h"
#include "header2.h"
...

When the preprocessor starts reading source.c, it sees the first include directive and loads the contents of header.h. The first time this happens, the macro HEADER has not yet been defined. The preprocessor defines HEADER and processes the contents of header.h. Then it sees the second include directive and loads the contents of header1.h. This in turn causes the contents of "header.h" to be loaded again. This time, however, the HEADER macro is already defined, so the contents of "header.h" are not processed again. Same thing happens when header2.h is processed.

This allows you to include whatever headers you need whereever you need them without having to worry about multiple declaration errors or maintaining dependencies between headers in the source files.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
gypsumfantastic":d19ftpfw said:
C/C++ have resolutely refused to incorporate anything as useful as a module system, so you're forced to employ an ugly preprocessor hack to stop things from being defined/compiled more than once.
Well, not anymore. Everyone supports #pragma once (which is still ugly, but not a preprocessor hack).
 

cnahr

Wise, Aged Ars Veteran
109
The standards committee considered proper module support for C++ 11 but couldn't agree on how to make it work. They'll consider it again for the next version but I'm not optimistic. That would be a huge change that's completely incompatible with all current C++ projects and libraries, so adoption would likely be poor. They'd effectively make a new language at that point.
 

gypsumfantastic

Ars Tribunus Militum
2,935
Subscriptor
Blacken00100":1qvicmmi said:
gypsumfantastic":1qvicmmi said:
C/C++ have resolutely refused to incorporate anything as useful as a module system, so you're forced to employ an ugly preprocessor hack to stop things from being defined/compiled more than once.
Well, not anymore. Everyone supports #pragma once (which is still ugly, but not a preprocessor hack).

Aye, that's a compiler hack. Which is definitely a better class of hack. :)
 
Status
Not open for further replies.