Pieter
Pieter

Reputation: 32805

Modifying qmake file to build to multiple targets for use with '#IFDEF macro'

I want to modify my Qt project's qmake file in such a way that it builds two versions of my program: one where SOME_FLAG is defined and one where it is not. My code will behave differently depending on the result of #ifdef SOME_FLAG.

Is this possible?

Upvotes: 5

Views: 1404

Answers (2)

casualcoder
casualcoder

Reputation: 5010

To the best of my knowledge, qmake only allows one TARGET, with one exception. That being, if you want to build a debug version and a release version, it is possible to build both with the same project file. This way, you can also specify the DEFINES for each build separately. Keep in mind that you can use the strip command to remove debugging after the fact, and maybe this will be usable for your circumstances. The Qt4 HTML docs (look to see if they are installed on your system) describes the debug_and_release mode in qmake-common-projects.html .

Now that said, you are allowed multiple project files. Create one project for each executable, with the desired DEFINES for each project. Use the qmake -o flag to issue separate Makefiles for each target and one Makefile to bind them. Can't help you with QtCreator, because I do not use it, but this works on the command line. A sample Makefile that illustrates this scheme would look something like this:

all: Makefile_A Makefile_B
        $(MAKE) -f Makefile_A
        $(MAKE) -f Makefile_B

Makefile_A: withSomeFlag.pro
        qmake -o $@ $<

Makefile_B: withoutSomeFlag.pro
        qmake -o $@ $<

This is just a quick file that does the job, somebody better at Makefiles might make it more generic. NOTE: the Makefile indentations are single TAB characters, not 8 spaces.

Also note that, by default, the executable name is the same as the basename of the project file. Hope that this gets you going in some capacity.

Upvotes: 2

Paolo Capriotti
Paolo Capriotti

Reputation: 4072

You can add

DEFINES += "SOME_FLAG"

in your .pro file, presumably within a conditional.

Upvotes: 1

Related Questions