#include best practices


What are best practices to manage header includes in Juce projects?

Many people over the net advise to include as few headers as possible in each file and to avoid one big include file in C++. However, it's just what JuceHeader.h and modules' headers do. I get fine compile times with Juce, despite having this header on all my files, but my projects are small yet.

So, how should I proceed with my projects' headers? Should I create this kind of big include file, or should I include headers one by one?


In juce itself, I use the "one big header" approach because otherwise it would be a total nightmare for everyone to keep track of which of the many hundreds of include files you need for each of your cpp files, and to get their order correct, and if any of the internal file names are added/removed/changed, then everyone's code would break. So it's easier for everyone the way it's done!

But in your own code, you probably want to take the other approach and minimise including your own headers as much as possible. Not really because of compile speed, which is unlikely to be an issue, but because the fewer dependencies there are between your files, the cleaner your code will be. If you have tangled code where you find all of your cpps needing to include huge lists of headers, then it's a danger sign.




I find if you just keep adding #includes it will eventually compile....it's quantity rather than cunning that's important.