DEFINES in QT Creator

  • Hi,

    I am trying to pass value of a DEFINES from Qt creator to source code. I am passing following line in Qt Creator

    Click on link Image.

    But when I use following line of code

    qDebug () << MY_DIR;

    I get

    error: C2065: 'username': undeclared identifier.

    I also tried

    DEFINES+='MY_DIR "username"' but it gives some other error. If I set the value to an integer it works fine like DEFINES+=MY_DIR=541. But setting the value to string is not working :(

    What is wrong here?

  • Lifetime Qt Champion


    IIRC, the windows shell needs something a bit more complex to handle quoted string.

    However, are you sure that MY_DIR should be a define ? From what you wrote it seems it should rather be something that you can change at run time, no ?

  • @SGaist Thanks for your time. No, not at run time. I only need to set while building\installing the application. It is not working on Linux either :(

  • Lifetime Qt Champion

    Can you give more details about what you do with that define in your application ?

  • @SGaist the define contains the path to a folder. In my Qt application I am using the define to read files in that folder and display the text of the files read. I want this to be a define so that user can pass this as an argument to qmake while building\installing the application.

  • Lifetime Qt Champion

    Well, that sounds rather like something that your user should either configure as command line parameter or read from a configuration file. Otherwise if he wants to change that folder for whatever reason he may had, he won't need to re-build/re-install the application.

  • Yes you are right but user cannot change the location of the folder once application is installed so a define can be used in this scenario. But I am unable to do it. Can you please give any example for doing it?

  • Lifetime Qt Champion

    Why can't he change that ?

    Try with:

    MY_DIR_STR = '\\"$${ZE_DIR}\\"'                                   
    DEFINES += MY_DIR=\"$${MY_DIR_STR}\"

    Call qmake like that

    qmake ZE_DIR=whatever

  • Qt Champions 2017

    I did
    in my profile to know where project is.

    To use it as a string i had to do the following

    #define _STR(x) #x
    #define STR(x) _STR(x)

    int main(int argc, char** argv) {
    const char* ProjLocation = STR(PROJECT_LOCATION);

    without the stringify macro (STR) it would see the actual path and give compile errors.

  • @SGaist Thanks for your reply. It is working :)
    It is a kind of restriction on user I know it is not good practice.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.