Search code examples
c++qtqt-creatorqmake

Managing a project with header-only libs in Qt


Right now I am having a project which uses a templated, hence header-only, library, and builds two apps.

My folder organisation is as follows :

base/lib
base/app1
base/app2

What I do right now is that I have base/lib/lib.pri which looks like :

HEADERS += ../lib/someHeader.h

And I include it in base/app1/app1.pro and base/app2/app2.pro.

However, in QtCreator, it looks somewhat ugly, for two reasons :

  • The path in the .pri file depends on where it is included from. For instance, if I had another app that for some reason I wanted to be in folder anotherBase/app3/ it would not work.
  • On the project tree it looks like :

(looks like I have to put text after a list in order to have code pretty-print)

app1
  -> app1.pro
  -> headers
  -> source files
  -> lib/
       -> lib.pri
       -> headers
           -> someHeader.h
app2
  -> app2.pro
  -> headers
  -> source files
  -> lib/
       -> lib.pri
       -> headers
           -> someHeader.h

Hence I have two times the library files in the tree view.

Is there any way to optimize this ? I would like to have :

app1
  -> app1.pro
  -> headers
  -> source files
app2
  -> app2.pro
  -> headers
  -> source files
lib
  -> lib.pri
  -> headers

in the tree view, however if I assing a .pro to lib and make it its own project, then when I want to compile everything it will complain that there is nothing to compile for lib and make an error...

Thanks!


Solution

  • In your .pri file, you should reference all paths to $$PWD. This variable contains the full path of the currently parsed file - namely, your include file.

    Note that PWD has different meaning than _PRO_FILE_PWD_, even if they occasionally return the same value.