Search code examples
visual-studio-2015qmake

qMake: how to define a variable for macro usage in C++


I am trying to specify variable in my qMake *.pro file for macro usage in my actual code (VS 2015 project).

I do have working examples like:

DEFINES += COPYRIGHT_STRING="\"$${QMAKE_TARGET_COPYRIGHT}\""
DEFINES += VERSION_STRING="\"$${QMAKE_TARGET_PRODUCT} $${VERSION}\""

I also want a macro for the string 3.4. I tried various versions to write that down now but I always get the following error.

RC : fatal error RC1107: invalid usage; use RC /? for Help

I tried the following variants and probably one or two more I do not remember anymore.

DEFINES += "LATEST_PROCESSABLE_VERSION=3.4"
DEFINES += LATEST_PROCESSABLE_VERSION=\"3.4\"
DEFINES += LATEST_PROCESSABLE_VERSION="\"3.4\""
DEFINES += LATEST_PROCESSABLE_VERSION=\\\"3.4\\\"

If I just omit that line defining the variable LATEST_PROCESSABLE_VERSION everything compiles just fine (which includes the working examples above).

My file generally looks like this:

win32:TEMPLATE = vcapp
!win32:TEMPLATE = app

QT += core xml network
CONFIG += console
CONFIG += no_keywords

#C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
QMAKE_CXXFLAGS += /wd4290

VERSION = 3.2.0.3

QMAKE_TARGET_COMPANY = APP GMBH

QMAKE_TARGET_PRODUCT = Fancy Application

QMAKE_TARGET_DESCRIPTION = Does fancy stuff

QMAKE_TARGET_COPYRIGHT = COPYRIGHT (C) $${QMAKE_TARGET_COMPANY} 2018 - ALL RIGHTS RESERVED - FOR INTERNAL USE ONLY

DEFINES += DECODER_COPYRIGHT_STRING="\"$${QMAKE_TARGET_COPYRIGHT}\""
DEFINES += DECODER_VERSION_STRING="\"$${QMAKE_TARGET_PRODUCT} $${VERSION}\""
DEFINES += LATEST_PROCESSABLE_VERSION="\"3.4\""

# set application icon
RC_ICONS = Application.ico

INCLUDEPATH += ../FancyInclude
# ... more includes ...

HEADERS += fancy.h \
# ... many headers ...

SOURCES += main.cpp \
    fancy.cpp \
# ... many sources ...

contains(DEFINES, APP_ARCH_x32) {
    DESTDIR = ../../product/win32
    CONFIG(debug, debug|release) {
        OBJECTS_DIR = ./debug_x86
        MOC_DIR = ./debug_x86
        LIBS += -L../addon/Fab/Bin32 -llibfab32D
        TARGET = FancyApp32D
    } else {
        OBJECTS_DIR = ./release_x86
        MOC_DIR = ./release_x86
        QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO
        QMAKE_LFLAGS_RELEASE = $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO
        LIBS += -L../addon/Fab/Bin32 -llibfab32D
        TARGET = FancyApp32
    }
}

contains(DEFINES, APP_ARCH_x64) {
    DESTDIR = ../../product/win64
    CONFIG(debug, debug|release) {
        OBJECTS_DIR = ./debug_x64
        MOC_DIR = ./debug_x64
        LIBS += -L../addon/Fab/Bin64 -llibfab64D
        TARGET = FancyApp64D
    } else {
        OBJECTS_DIR = ./release_x64
        MOC_DIR = ./release_x64
        QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO
        QMAKE_LFLAGS_RELEASE = $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO
        LIBS += -L../addon/Fab/Bin64 -llibfab64D
        TARGET = FancyApp64
    }
}

After an intensive google search I am now running out of options to try. Can anybody please assist?


EDIT 1:

At least I thought I had those examples at the top working for once. But I am currently even failing on making them work (again) and I am completely lost on what is causing the errors here. I know nothing anymore, please help! -.-


EDIT 2:

Okay it seams the heat has taken its toll on me. The actual problem are the spaces in QMAKE_TARGET_COMPANY and QMAKE_TARGET_PRODUCT. The following example is now working fine.

...
QMAKE_TARGET_COMPANY = Test GmbH

QMAKE_TARGET_PRODUCT = Fancy Application

QMAKE_TARGET_DESCRIPTION = Does fancy stuff

QMAKE_TARGET_COPYRIGHT = 2018

DEFINES += COPYRIGHT_STRING="\"$${QMAKE_TARGET_COPYRIGHT}\""

DEFINES += VERSION_STRING="\"$${VERSION}\""

DEFINES += LATEST_DECODABLE_ESF_VERSION="\"3.4\""    
...

I would like...

...to use spaces in QMAKE_TARGET_COPYRIGHT to have the value COPYRIGHT (C) $${QMAKE_TARGET_COMPANY} 2018 - ALL RIGHTS RESERVED - FOR INTERNAL USE ONLY

and I would like...

...to define another variable APP_NAME_AND_VERSION_STRING consisting of $${QMAKE_TARGET_PRODUCT} and $${VERSION} separated by a space.

How can I achieve this? I did not find anything to make it work.


Solution

  • Keep in mind that the text must be escaped twice: for qmake itself and for the shell command. So the proper escaping should be like this:

    DEFINES += LATEST_PROCESSABLE_VERSION=\\\"3.4\\\"
    

    and

    DEFINES += APP_NAME_AND_VERSION_STRING="\\\"$${QMAKE_TARGET_PRODUCT}\ $${VERSION}\\\""
    

    Note that all spaces in the string must be escaped.

    You can also switch to using QMAKE_SUBSTITUTES to auto-generate an appropriate C header file and to avoid ugly command-line DEFINES.