How to organize source directory

This is a nooby question, but the more code I write the more I would love to find a working solution:

Is there a way to create subdirectories inside of the ‘src’ directory of a program, in order to better organize its components? I’m on a debian machine, using qtCreator. I’m guessing that it is not allowed to create such subdirectories and that I will have to build components separately and somehow include them in my main program? If so, how to do it?

Thank you guys for your help!

Yes you can make subdirectories.

  • Create a subdirectory in the src directory (with your usual file manager, or a mkdir command)
  • Create a new file or class with the appropriate qtcreator wizard. Take care to select your new directory within the wizard.
  • You can also create or copy some source code files in the new directory and add them to the qtcreator project by righ-click on the project folder in the qtcreator project pane, and then choose “add existing files…” in the contextual menu.
  • At this state, the new files are compiled when you build the project.
  • To include these source files in others source files you have to do

#include "theNewDir/theSourceFile.h"

You can simplify this by adding this new directory in the include path. To do that edit the .qbs file at the root of your project and add the directory name to the of.includePaths variable:

of.includePaths: [
]     // include search paths

When done you just have to do

#include "theSourceFile.h"

to use the source files from this dir.

Lilive, Thank you very much. This worked perfectly. Before, I could not get the directory to appear in the project sidebar, which led me to assume that I was not allowed to create subdirectories, so I just kept going with what worked at the time. I’m so excited to establish order in my source directory!

+1 Experience Point. TY