Important: Please read the Qt Code of Conduct -

[SOLVED] Having custom variables in Makefile

  • Hello,

    I have a .pro file. That needs a path. For example:

    Project file

    ROOT = dummyPath
    MYPATH = $(ROOT)/myappPath

    $(ROOT) will be coming from my Makefile.

    I have a custom Makefile, that runs qmake. Example:

            qmake -o Makefilegen

    I want to take ROOT from my Makefile and use it in .pro file and finally while generating the Makefile from .pro file, I would like to have something like this:

    MYPATH = /actualPath//myappPath

    How to do this?

    I know I am trying to solve my own problem here.
    But the basic questions are

    • How to pass a variable to qmake
    • How to make that variable to be part of generated Makefile.

    Please let me know.

    Thank you,

  • Haven't tried it, but check out the discussion here.

  • Lifetime Qt Champion


    Do you mean something like:

    TEMPLATE = app
    TARGET = test
    # Input
    SOURCES += main.cpp
    DEFINES += MyData=$${TEST}
    message(Environment Variable:  $${TEST})


  • Hi,

    Later to a quick search and experiments, I have got what I have wanted but partially.

    Here are the details:

    My Makefile looks like this:

    export BOOST_LIBS_PATH=/path/to/boost
            qmake -o Makefilegen

    As it says, it exports a path, and creates Makefilegen using looks like this:

    TEMPLATE = app
    TARGET = myapp
    # Input
    SOURCES += main.cpp
    message("Master pro file path : ["$(BOOST_LIBS_PATH)"]")
    MYTESTPATH = $(BOOST_LIBS_PATH)/er/er/er/e
    message("My path : ["$${MYTESTPATH}"]")

    And in the console

    Project MESSAGE: Master pro file path : [/path/to/boost]
    Project MESSAGE: My path : [/path/to/boost/er/er/er/e]
    Project MESSAGE: My path : [/lib/aaa/bbb]

    But, I want to take this to my source code. But it doesn't work


    In the generated Makefile


    I wish to get as


    How do I pass a variable to defines?

  • Lifetime Qt Champion

    You'll have to escape your string more than that:

    TESTSTR = '\"$$(TEST)\"'

    SOURCES += main.cpp

    [edit: fixed code sample SGaist]

  • @SGaist Thanks Sam.

    With your thoughts,

    DEFINES += WAT=$${WAT}

    And this is how the generated Makefile looks like:


    What I see here is, with the local variables, it looks to be working.

    but the environment variable, I am not able to acheive.
    -DGCPU_PATH=$(BOOST_LIBS_PATH) I expect this to be -DGCPU_PATH="/path/to/boost"

    Any more thoughts?

    Thank you,

  • Lifetime Qt Champion

    Sorry, it seems that the code highlighter is not working exactly the same when you are editing the answer and once it's re-loaded.

    I've fixed it. Please take a look again, it's already working for environment variables.

  • Sam, I am bit confused.

    If I understand correctly,

    Having your code,

    TESTSTR = '\\"$\\"'
    DEFINES += TEST=\"$\"

    Should I need to add my environment variable here?

    TSTR = '\\"$(BOOST_PATH)\\"'

    Makefile looks like this:


    Or I understood wrongly?

    Please clarify. Thank you very much for your kind help..


  • Lifetime Qt Champion

    Sorry again about the code formatting…

    Trying again here:

    TESTSTR = '\\"$$(TEST)\\"'
    DEFINES += TEST=\"\$\$\{TESTSTR\}\"

    On that last line remove the backslashes before the dollar signs and curly braces...

  • I did try the exact as you said..
    STRT = '\\"$(BOOST_PATH)\\"'
    DEFINES += BOOST_PATH=\"\$\$\{STRT\}\"
    And the output is,

    And I tried removing the back slashes on dollar sign and curly braces
    STRT = '\\"$(BOOST_PATH)\\"'
    And the output is

    Still no success..


  • Lifetime Qt Champion

    What do you get for the various variable if you show a message with their content:



    Again, remove the backslashes in the second line...

  • While printing a message, it looks good.


    Project MESSAGE: /path/to/boost


    Project MESSAGE: \"/path/to/boost\"

    But why this has not been taken to Makefile? Mysteries..

  • Lifetime Qt Champion

    Then you are likely just missing some escaping for the last call...

  • Lifetime Qt Champion

    Finally found a way to write it:

    BOOST = $ $ ( BOOST_PATH )
    BOOST_STR = ' \ \ " $ $ { BOOST } \ \ " '
    DEFINES += BOOST_PATH= \ " $ $ { BOOST_STR } \ "

    Extra spaces to be removed for the sample code

  • Thanks Sam, for helping me to fix the problem.

    I do confirm that, it works.

    (I do not want copy the instruction again here, which contains more spaces :P)

    With the above code, I can see this in my Makefile


    It has been a great help! Thanks a lot.


  • Later to that,

    I have figured out a problem in having:

    We need to have \" in the argument:
    Problem is that, the macro turned out to be
    #define BOO_PATH /path/to/boost
    which is actually useless. We need to have double quotes to cover them up.

    BOOST =  $ $ ( BOOST_PATH )
    BOOST_STR = ' \ \ " $ $ { BOOST } \ \ " '
    DEFINES += BOOST_PATH=\ " $ $ { BOOST_STR } \"

    Again, eliminate the spaces :)

Log in to reply