2

Hi I am relatively new to Qt and I could not find a viable solution to this so far. I have the following project structure

ALPHA-app
    alpha.pro
    ALPHA-Desktop
        alpha-desktop.pro
    ALPHA-Shared
        alpha-shared.pro
    ALPHA-Common
        alpha-common.pro
    ALPHA-Tests
        alpha-tests.pro

I basically have to run alpha.pro which builds all the projects and creates an executable

alpha.pro,

TEMPLATE = subdirs
DESTDIR = .
SUBDIRS += \
    ALPHA-Common \
    ALPHA-Shared \
    ALPHA-Desktop \
    ALPHA-Tests

ALPHA-Desktop.depends = ALPHA-Common
ALPHA-Shared.depends = ALPHA-Common
ALPHA-Tests.depends = ALPHA-Common
ALPHA-Tests.depends = ALPHA-Desktop

the main executable that will be run is present in the ALPHA-Desktop project (given below)

TARGET = ALPHA #this is in ALPHA-Deskop.pro
TEMPLATE = app

Now I created ALPHA-Tests to make a project to run unit-tests. It depends on ALPHA-Desktop (Hence I wrote the .depends statement in the ALPHA project)

To run the ALPHA-Tests, I need to create a static library of the ALPHA-Desktop folder. Hence I wrote the following lines in ALPHA-Desktop.pro file

TARGET = ALPHA-Desktop #section 1
TEMPLATE = lib         #section 1
CONFIG += staticlib    #section 1

TARGET = ALPHA         #section 2
TEMPLATE = app         #section 2

When I run the ALPHA.pro file, the ALPHA-Desktop library is not getting created. Only the ALPHA executable is being created. If I interchange the positions (i.e., put section 2 above section 1) then only the libALPHA-Desktop.a file is created and the executable is not created.

How do I solve this? (I am using qmake version 3.0, Qt version 5.4.2 on ubuntu 15.10)

clearScreen
  • 1,002
  • 4
  • 15
  • 31
  • For that kind of a setup I suggest you to switch to CMake. I've always run into problems when trying to do something similar with qmake. CMake also has the concept of subprojects - the difference is that it actually works properly. Also QtCreator supports CMake out-of-the-box. – juzzlin Mar 22 '16 at 12:33
  • I am required to use qmake. Is it not possible to do this using qmake? – clearScreen Mar 22 '16 at 13:10
  • I don't think you can build multiple targets in a single .pro file like you're doing in ALPHA-Desktop.pro. The second TARGET=... always overwrites the previous TARGET=... You should re-arrange that somehow...maybe you could just build the static lib always in a separate .pro file and add it to the SUBDIRS? – juzzlin Mar 22 '16 at 14:16
  • Actually I was wrong about multiple target in the same file, check this: http://stackoverflow.com/questions/2259192/building-multiple-targets-in-qt-qmake However, I'm still not sure if you can build both configurations at the same time. – juzzlin Mar 22 '16 at 14:20
  • @juzzlin , I looked at that thread before. I made a .pri file. But when I included in the ALPHA-Tests.pro file, I am unable to make a build. I get , "source.cpp not found" error – clearScreen Mar 22 '16 at 15:20
  • 1
    qmake will not allow you to create two targets in the same .pro file unless you explicitly use conditional build as it is shown, for example, on the link in the comment above. What I would recommend in such situation is using two .pro files (and subdirs) - one for static desktop library and one for final desktop application, and the second one just compiles main.cpp and links with the library compiled on the first step. With this approach you can build as many applications using desktop library as you need - just create new subdir and .pro for each app (main app and tests in your case). – Evgeny S. Mar 22 '16 at 19:04

1 Answers1

2

TEMPLATE, TARGET etc are always global for one .pro file. While this is never explicitly spelled out in the qmake documentation, it is obvious once you realize that the qmake syntax aims to be declarative. The last line setting a variable will always prevail (except if you overwrite the variable via the command line).

If you want to create a static library and an executable, you need to have two .pro files (probably again integrated with a subdirs .pro file).

Note though that this doesn't necessary mean that you need different subdirectories, since multiple .pro files can also exist in the same directory:

ALPHA-Desktop.pro:

TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += ALPHA-Desktop-staticlib.pro ALPHA-Desktop-app.pro 

ALPHA-Desktop-staticlib.pro:

TEMPLATE = lib
CONFIG += static
# ...

ALPHA-Desktop-app.pro:

TEMPLATE = app
# ...
kkoehne
  • 1,176
  • 9
  • 17
  • 1
    When multiple .pro files exist in one directory how qmake knows which one to use when it iterates over SUBDIRS? Doesn't it take the .pro file which base name is the same as the directory name? – Evgeny S. Mar 22 '16 at 20:01
  • 1
    You can also list .pro files directly in SUBDIRS. Extended the answer. – kkoehne Mar 22 '16 at 20:12
  • I didn't know that we could add multiple project files. Thanks! – clearScreen Mar 23 '16 at 15:27