#Handling header file being included multiple times

17 messages · Page 1 of 1 (latest)

round nexus
#

I have a header file called util.h which contains some useful general functions I use throughout all my source files, thus I need it included in (almost) every source file. I also have other header files, which often also include this util.h file. Now what's happening is that I'm accidentally including util.h multiple times, by for example including two different header files which both include util.h. I've read about #ifdef guards which can help to solve this problem. But I'm interested in knowing if having such a general header file even is a good idea? if not, how would you tackle this problem otherwise? and if it indeed is a good idea, what is the preferred way of handling the issue if including the same header file multiple times in the same source file?

sonic vaultBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

round nexus
#

Handling header file being included multiple times

grizzled zodiac
#

Yeah it’s fine, it’s like how <fstream> and <sstream> both include <ios>

#

Include guards as you mentioned is good, you can also use #pragma once if you’re lazy

#

It’s technically not official but almost all compilers support it

round nexus
#

hm alright, do you have a good resource at hand about ifdef guards as I not know much about them? if not it's fine I'll be able to find something on my own

grizzled zodiac
#

Um it’s just
#ifndef FUNNY_MACRO_NAME
#define FUNNY_MACRO_NAME
// actual contents
#endif

round nexus
#

ah lol

grizzled zodiac
#

Basically if the macro isn’t defined, then it’ll define it and include the actual contents

#

If it is already defined, then all of that is skipped

round nexus
#

ah I see

#

and I put that around all #include's?

#

where I need it*

#

yeah I guess

#

alright thanks :)

#

!solved