Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if a header has already been included before [duplicate]

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")
like image 604
Alessandro Soares Avatar asked Jun 08 '13 15:06

Alessandro Soares


2 Answers

You are looking for include guards.

Example,

#ifndef DEFINITIONS_H 
#define DEFINITIONS_H 
...
...

#endif
like image 184
P.P Avatar answered Sep 17 '22 15:09

P.P


#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?

like image 22
Yu Hao Avatar answered Sep 20 '22 15:09

Yu Hao