Why do some tutorials use Main.cpp & Component.h versus Audio App Template...?

I am kind of new to JUCE and trying to wrap my head around the framework for making some I/O type of apps.

Why do the tutorials like ‘ComponentsParentsChildrenTutorial’ and ‘LoopingAudioSampleBuffer’ use only a Main.cpp and a .h file where as when creating a new Audio Application from the Projucer templates, the default setting is to create a Main.cpp + .h/.cpp…then there’s even a discrepancy in the 'Tutorial:Projucer Part 3: Choosing the right Projucer template for your application ’ where it explains that the Audio App template will include a Main.cpp and MainComponent.cpp.

Is there any rhyme or reason that I can pick up on from this difference for developing my own apps? Is there a convention for when to merge header and implementation into a single .cpp or .h?

Any insight would be great! Thanks
-Mike

Boosting this because I think it deserves an answer for us Juce newbies :^)

There is no clear answer. You can organise your files as you like them, each with advantages and disadvantages.

  • for each cpp file there will be one compiler started (the juce modules are different though)
  • header files (.h or .hpp) are copied into the cpp files with the include statement
  • header files are responsible to make sure they are skipped, if they are included twice
    • either with #pragma once
    • or with an #ifndef FOO_H_ block
  • when all cpp are compiled to .o files, they are linked to the binary

If you put everything in headers, chances are it is compiled multiple times by different translation units (the compiler instance for each cpp). The linker has to sort that out.

Stuff in header can be better optimised, because the compiler has access to the implementation and can inline and other tricks.

For anything else there is still the link time optimisation, which is slower though.

TL;DR: the authors of the different tutorials had different preferences about that topic.

1 Like