qtbuildproject-organization

Organize Qt project files in folders, and simplify the includes


Brief

How do I tell my sub-project which are its subdirectories, in pro file, and not have to prefix the subdirectories in each #include directive ?

Problem description

I have a large project, containing multiple libraries. Also, I am looking at one library that has a large number of classes. I am trying to organize it in such a way that it is easier to see everything at a glance.

I am using Qt Creator. The only way to make the logical grouping I am thinking of, is to create folders.

enter image description here

The "pro" file looks logical:

SOURCES += sample.cpp \
    class1.cpp \
    class2.cpp \
    interfaces/interface1.cpp \
    interfaces/interface2.cpp

In each file that I must include one of the headers I must also add the folder:

#include "interfaces/interface1.cpp"

I would not give this a second thought, except...

For an outside library or application, I can add in their pro file (or include a pri file) to tell where the current library files are located:

INCLUDEPATH += $$PWD/../Sample
INCLUDEPATH += $$PWD/../Sample/interfaces

This creates the magic: from any outside library or application, I don't need to prefix the include directive with the folder location.

#include "interface1.cpp"

I have not been able to make this work from the current project. I don't understand why, I would like to know how to make it so that I no longer need to ad the folder path in the include directives (while still keeping the visual logical organization of my files).

Summary

For the structure above, adding

INCLUDEPATH += $$PWD/../Sample/interfaces

In the po file for an outside project allows me to include files like this:

#include "interface1.cpp"

I want to be able to do the same from the current project files. Right now, I am forced to include files like this:

#include "interfaces/interface1.cpp"

Solution

  • Create .pri file for subdirectory. And include .pri in .pro file.