35

I'm writing a small qt app suite that consists of a set of small programs that work on the same set of files. They are organized like this:

/
  app1/
    main.cpp
  app2/
    main.cpp
  app3/
    main.cpp
  common/
    project.h
    project.cpp
    somemore.h
    somemore.cpp
  appsuite.pro

When I do qmake && make, I want the following binaries to be built:

  • app1/app1
  • app2/app2
  • app3/app3

How do I write appsuite.pro to work like this?
I have heard something about .pri files, but I could not figure out how to use them in my "situation".

Help appreciated,
jrh

jrharshath
  • 25,975
  • 33
  • 97
  • 127

2 Answers2

37

One way of doing it is to have a .pro file per subdirectory.

appsuite.pro:

TEMPLATE = subdirs
SUBDIRS = common app1 app2 app3
app1.depends = common
app2.depends = common
app3.depends = common

app1/app1.pro:

TARGET = app1
SOURCES = main.cpp
INCLUDEPATH += ../common
LIBS += -L../common -lcommon

The common.pro file should build a static library you can then link into the binaries.

common/common.pro:

TEMPLATE = lib
CONFIG = staticlib
SOURCES = project.cpp more.cpp
HEADERS = project.h more.h
PiedPiper
  • 5,735
  • 1
  • 30
  • 40
  • 1
    A .pri file merely contains other qmake commands. Certainly they often list the files for inclusion in a project, but it isn't necessarily so. For example, at work we have one that defines a target so that we can do a "make depends" and have the makefiles regenerated. – Caleb Huitt - cjhuitt Oct 09 '09 at 02:34
  • @cjhuitt .pri files weren't really relevant in this case so I've removed the reference. – PiedPiper Oct 09 '09 at 09:23
  • After using this solution in my "Common" subdir project I get errors for missing Qt Libraries like: `Common/localserver.h:4:19: fatal error: QThread: No such file or directory` – Itay Grudev Dec 31 '12 at 17:56
  • 2
    Additionally, you can use a `common.pri` that is included from the `app*.pro` files to centralize the `INCLUDEPATH` and `LIBS` directives. – rubenvb Mar 20 '15 at 12:32
  • Ok... For my case i think create subdirs for applications (e.g. ./app/* and ./app-cli/*) with *.pro separated files and create here symlinks like "./dependency-sources -> ../dependency-sources and build it as separated executables with using same source components. This is bad idea or not? – Deep Jan 21 '18 at 10:04
  • @PiedPiper i make it without symlinks (for compatible on platforms): 3 subdirs: cli, core, gui. core dir have core components, and cli, gui dirs have main-cli.cpp and main-gui.cpp (and other files for self), 3 *.pro files (cli, gui and linguist) in other subdir. It make 2 standalone apps (gui build first) and cli make using same core *.o files without recompile it. And after i run linguist for all sources. It works great. – Deep Jan 28 '18 at 20:46
11

One way is to create your global project appsuite.pro, like this:

TEMPLATE = subdirs
SUBDIRS = app1 app2 app3

The subprojects app1.pro and app2.pro should also be created for those applications alone, with a dependency regarding the common/ subdirectory

You can also specify other dependencies in appsuite.pro, for example if app1 depends on app2, as:

app1.depends = app2
RedGlyph
  • 11,309
  • 6
  • 37
  • 49