How to test if a header has already been included before
10,765
Solution 1
You are looking for include guards.
Example,
#ifndef DEFINITIONS_H
#define DEFINITIONS_H
...
...
#endif
Solution 2
#ifndef DEFINITIONS_H
#define DEFINITIONS_H
//lots of code
//
//
//
//
#endif
There's also non-standard #pragma once
, see Is #pragma once a safe include guard?
Solution 3
If your header syntax is correct, this should not be a problem. In fact, this is the reason why you write
#ifndef _DEFINITIONS_H
#define _DEFINITIONS_H
[header content]
#endif
So, if your header is conform to C conventions, you should be fine.
Author by
Alessandro Soares
Updated on July 28, 2022Comments
-
Alessandro Soares almost 2 years
I'm making a project, and I get stunned with a problem.
I have 3 libraries.h that includes another special library, definitions.h, but in my Main module, i want to include all the libraries just one time, I mean, I want to test if the library
definitions.h
has already been included, and include it or not depending on the result.Something like
If !(#include"definitions.h") (#include"definitions.h")
-
Kninnug almost 11 years+1 for beating me to it.
-
pinkpanther almost 11 years+1 beating me to it...Add some more explanation...it seems the OP needs it...
-
hellerve almost 11 years+1 for being faster than me.
-
hellerve almost 11 yearsOT: Interesting to see that I seem to be the only one using leading underscores for my
#define
. -
Jens almost 11 years+1 for not using identifiers starting with underscore+caps (undefined behavior)
-
Jens almost 11 yearsThis is undefined behavior because you are using an identifier starting with underscore+caps. This should be
DEFINITIONS_H
. You seem to be the only one who hasn't seen the light yet :-) -
EvilTeach almost 11 yearsYa. Arn't they kind of meant to be reserved to the compilers namespace?
-
hellerve almost 11 yearsI'm not sure tbh. I trusted Zed Shaw(author of "learn C the hard way") there. He always does it like this and I just adopted it. So, @jens, are you completely sure?
-
chux - Reinstate Monica almost 11 yearsIOW, leading underscore appears OK as long as it is not followed by an uppercase letter/underscore. Such could be used in the future by the compiler. Ref: C11 draft 16.8.4: "Any other predefined macro names shall begin with a leading underscore followed by an uppercase letter or a second underscore."
-
Alessandro Soares almost 11 yearsThanks, my problem is solved
-
Jens almost 11 yearsYes I'm sure. The C Standards forbids the use of reserved identifiers. Underscore+Caps is always reserved for any use = in any namespace, even in the macro namespace. Shaw is wrong because he just wrote it like he found it in the implementation headers in /usr/include.
-
hellerve almost 11 yearsThanks for the clarification. Always nice to learn something new.
-
kot_mapku3 about 4 yearsOk, but does it work if one of them exist and another no? It will decline all of them?
-
P.P about 4 years@kot_mapku3 hi, it's not clear to me what you're referring to "one of them exist and another no". Can you clarify or give an example?
-
kot_mapku3 about 4 years@P.P I mean e.g. (header1 and header2 have a check "ifdef..." etc.) header1.h: #include <iostream> and #include <vector>, header2.h: #include <iostream> and #include <set> and main.cpp: #include "header2.h" #include "header1.h". Will main.cpp contain both of them or just one of them? Clearly: it will contain 3 headers libraries or 2?
-
P.P about 4 years@kot_mapku3 It'll include both the headers. But since <iostream> is already included via header2.h, it won't be included via header1.h (as the include guard in <iostream> will prevent that). So main.cpp will have 3 includes: <iostream>, <vector>, and <set>).