简体   繁体   中英

How to pass variables to qmake project file when qmake is run

I need to dynamically change my eg application name and subsequent defines when calling qmake on my project file.

An example would be:

include(version.pri)

QT       += core gui network concurrent

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
FLAVOR = ?

TARGET = ../bin/$$FLAVOR
TEMPLATE = app

message("Application name: $$APP_NAME")

!defined(APP_NAME) {
    APP_NAME = $$FLAVOR
}

!defined(APP_ORG) {
    APP_ORG = myorg
}

//...

message("Application name 2: $$APP_NAME")

Output

Application name:
Application name 2: ?

Above is an extract from my project file.


Documentation:

According to Qmake documenation , I should be able to do the following:

FLAVOR = xyz

!defined(APP_NAME) {
    APP_NAME = $$FLAVOR
}

Purpose of above code:

Check if APP_NAME is defined (anywhere, even passed as qmake parameter). If it is NOT defined, then set APP_NAME equal to FLAVOR which contains xyz

I have tried:

  • qmake -config debug "DEFINES += APP_NAME = myappname"
  • qmake -config debug "APP_NAME = myappname"
  • qmake -config debug APP_NAME = myappname

of which none work.

How can I pass a definition to qmake which will set a variable in the Makefile ie APP_NAME as shown in the above example?

please note : some answers suggest of an issue with the question title. I have since edited the title to a more appropriate summary of my question and what I hope to achieve

I assume you are not trying to add DEFINES (as in preprocessor defines, passed to the compiler), but to "assign" variables for use inside the QMake project file.

The following works for me:

QT       += core gui network concurrent

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
FLAVOR = ?

TARGET = ../bin/$$FLAVOR
TEMPLATE = app

message("Application name: $$APP_NAME")

!defined(APP_NAME, var) {
    APP_NAME = $$FLAVOR
}

message("Application name 2: $$APP_NAME")

One seemingly crucial change to make the "defined"-statement work was to add "var" as a second parameter, as you can see. Not sure why, but without it, the second output was "?", while the first one was correctly "myappname".

The output of qmake being:

...\Desktop\test_qmake>qmake -config debug "APP_NAME = myappname"
Project MESSAGE: Application name: myappname
Project MESSAGE: Application name 2: myappname
Project MESSAGE: Application name: myappname
Project MESSAGE: Application name 2: myappname
Project MESSAGE: Application name: myappname
Project MESSAGE: Application name 2: myappname

...\Desktop\test_qmake>qmake -v
QMake version 3.0
Using Qt version 5.7.1 in C:/Qt/5.7/mingw53_32/lib

// EDIT: Regarding the "defined"-statement, the docs say "[...] If type is omitted, checks all functions.", where "type" is the second parameter. Seems to be taken literally, ie only functions are checked, but no variables. QMake function reference "defined"

You can pass defines to qmake like this:

qmake DEFINES+="APP_NAME myappname"

And if you want to add defines in a pro file you do like this:

DEFINES += "APP_NAME=myappname"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM