I am writing a program with multiple headers and code files, as so:
- message.h
- message.cpp
- option.h
- option.cpp
- main.cpp
message.h includes option.h as message.cpp requires the definition of the class defined therein.
There are quite a few other files, however just to keep it brief...
If I want to call a function such as memcpy, I include string.h. I include string.h in message.h - so it works fine in message.cpp, but if I want to use it in option.cpp it gives me an error.
So I see two options:
- In option.h, I include message.h again. This should not be an issue as I have include guards in both header files, however I feel it's rather messy.
- I include string.h in option.h. Now I am including string.h in both of my header files.
This gives me the declaration for memcpy and so I can get on with my programming. However when I'm including 10 different headers for many different functions, it's going to be messy again.
What is a good way for me to structure my code to get around this issue, to keep my code as neat as possible? It has crossed my mind to make all my includes in one separate file and include that in all my header files, could this be a problem in the future, and is there a better way I can go about this to ensure my code is maintainable?
It has crossed my mind to make all my includes in one separate file and include that in all my header files, could this be a problem in the future
- #include'ing the kitchen sink in each file works for small projects but if the project gets big it might harm compilation time. Try doing a benchmark to see the impact on your environment. Make sure you're using namespaces properly to avoid name conflicts and I can't see any maintainability problem with this approach.