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?
#Handling header file being included multiple times
17 messages · Page 1 of 1 (latest)
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.
Handling header file being included multiple times
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
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
Um it’s just
#ifndef FUNNY_MACRO_NAME
#define FUNNY_MACRO_NAME
// actual contents
#endif
ah lol