Search code examples
c++qtcmakeqmake

cmake analogues to qmake string literals


there is a .pri as a part of qmake project which contains some global string definitions :

STR1 = string literal 1
STR2 = string literal 2
GIT_VERSION = $$system($$ENV_GIT_EXE_PATH describe --long)

and these strings used in a code :

QString str1 = QStringLiteral(STR1);
QString str2 = QStringLiteral(STR2);
QString str3 = QStringLiteral(GIT_VERSION);

How to implement this functionality using cmake project? I can't change the sources (C++.h/.cpp) and have to prepare those strings somehow using cmake abilities


Solution

  • Qmake’s string literals are nothing but a preprocessor macro definitions. In CMake this can be done with add_compile_definitions or with target_compile_definitions

    So the analogue would be:

    add_compile_definitions(STR1="string literal 1")
    add_compile_definitions(STR2="string literal 2")
    execute_process(COMMAND ${ENV_GIT_EXE_PATH} describe --long
    OUTPUT_VARIABLE git_version)
    add_compile_definitions(GIT_VERSION="${git_version}")
    

    PS. You can access environment variables with $ENV{GIT_EXE_PATH}