Results 1 to 11 of 11

Thread: qmake generate user-defined header files, or pass variable definitions to program

  1. #1
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Question qmake generate user-defined header files, or pass variable definitions to program

    Hi, I'm trying to figure something out and have looked everywhere but don't see it referred to, or admittedly I could be using the wrong search term.

    I want to set a variable that is to be used by both qmake and my program. So far I have only figured out that I could put that variable in both my .pro file and my .h file, the first for qmake, and the second for my program's use. Is there either a way for my program to use variables set in the pro file(s), or if no such thing exists, is there a way for me to generate, on the fly, an automatically created header file that will be created with that variable/value as a #define? If I could do the first I would be very happy, but I would be gratified to hear that I could at least do the second.

    I was looking around and found that I can call qmake -set <variable> <value>, and I got really excited that I had found the thing. The web page even said that QSettings::value("<variable>") would be used to get the value, but I am now of the impression that's only for setting a global variable for the current environment, and not one to be passed in to the settings for my program. Also, that would be a command line thing that would be done separate to a regular qmake call that created Makefiles.

    Does anyone have any suggestions for me to not have to create two instances of the same variable in two different files, a pro file and a h file?

    Thanks...

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qmake generate user-defined header files, or pass variable definitions to program

    Its sounds to me that what you want (which I don't really fully understand) is bad, and should not be done.
    Try first explaining what it is you wish to achieve, we will probably be able to point you in the correct direction then.
    What it is you want to parametrize?
    Are you sure that what you want are not application arguments?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qmake generate user-defined header files, or pass variable definitions to program

    What's wrong with using qmake's DEFINES variable?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Post Re: qmake generate user-defined header files, or pass variable definitions to program

    Great idea. I hadn't realized I could pass a value along with a define given in a .pro file. I googled and found someone doing it (http://www.qtcentre.org/threads/29104-QMake-DEFINES), so I created a test project called tmp. When I compiled I got an error. My files follow:

    -------- tmp.pro -------
    TEMPLATE = app
    TARGET =
    DEPENDPATH += .
    INCLUDEPATH += .
    SOURCES += main.cpp
    DEFINES += "MYVAR=\"myvarvalue\""

    -------- resulting Makefile entry -----
    DEFINES = -DMYVAR="myvarvalue" -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED

    -------- main.cpp ------
    #include <QApplication>
    #include <QDebug>
    #define S "Smellypants"
    int main(int argc, char **argv) {
    QApplication a(argc, argv);
    qDebug() << S;
    #ifdef MYVAR
    qDebug() << "MYVAR is defined as: " << MYVAR;
    #endif
    return a.exec();
    }

    -------- compilation output -------
    g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DMYVAR="myvarvalue" -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../qtsdk-2010.05/qt/mkspecs/linux-g++ -I. -I../../../qtsdk-2010.05/qt/include/QtCore -I../../../qtsdk-2010.05/qt/include/QtGui -I../../../qtsdk-2010.05/qt/include -I. -I. -o main.o main.cpp
    main.cpp: In function ‘int main(int, char**)’:
    main.cpp:11: error: ‘myvarvalue’ was not declared in this scope
    make: *** [main.o] Error 1

    -----------------------------------

    Basically, I created a local #define in my main.cpp, and was able to print that string just fine (with the MYVAR print commented out). But when I tried to print MYVAR, it acts like as if the value (myvarvalue) is a code token rather than a variable value. I am not sure what the difference would be between accessing a local #define and accessing a -D define from the Makefile. I've never done this before. Any suggestions? I attached my full Makefile (as Makefile.txt) if you need to look at that.
    Attached Files Attached Files

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qmake generate user-defined header files, or pass variable definitions to program

    Your MYVAR is not a string literal. Make it a string literal (there are a couple of ways to do that, e.g. use the "#" preprocessor directive) and it will work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Wink Re: qmake generate user-defined header files, or pass variable definitions to program

    Great. I am not sure how I would use the # directive from a .pro file, but I changed my DEFINES to be this:

    DEFINES += MYVAR=\"\\\"myvarvalue\\\"\"

    and it worked perfectly. If you would, could you let me know what you meant by using the # directive from the .pro file?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qmake generate user-defined header files, or pass variable definitions to program

    Quote Originally Posted by banjoman View Post
    Great. I am not sure how I would use the # directive from a .pro file
    Not from the .pro file. The following should work.

    Qt Code:
    1. #define str(x) #x
    2. qDebug() << "MYVAR is defined as: " << str(MYVAR);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qmake generate user-defined header files, or pass variable definitions to program

    Hmmm, much cleaner than what I had done. Thanks for the clarification.


    Added after 12 minutes:


    Oops, spoke too soon. Just tested that and it prints:

    MYVAR is defined as: MYVAR

    I tried different variations of the tmp.pro definition, but they all do this, which seems to mean that the directive

    #define str(x) #x

    is not working correctly. Now I'm back to my uglier but working version. Are you sure this is working for you?
    Last edited by banjoman; 10th December 2010 at 15:20.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qmake generate user-defined header files, or pass variable definitions to program

    It should probably be:
    Qt Code:
    1. #define str_(x) #x
    2. #define str(x) str_(x)
    3. qDebug() << "MYVAR is defined as: " << str(MYVAR);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qmake generate user-defined header files, or pass variable definitions to program

    Eureka, that's it!!! Thanks!

  11. #11
    Join Date
    Apr 2011
    Posts
    1
    Thanked 1 Time in 1 Post

    Default Re: qmake generate user-defined header files, or pass variable definitions to program

    the following worked for me:

    (in .pro)
    Qt Code:
    1. VERSION = 3-alpha-10.2
    2. DEFINES += FOO_VERSION=\\\"$$VERSION\\\"
    To copy to clipboard, switch view to plain text mode 

    (I need the qmake VERSION variable in the project file because I use it elsewhere,
    FOO_ prefix of the define is to avoid clashing with something).

    (in .cpp)
    Qt Code:
    1. QLabel * version = new QLabel( FOO_VERSION );
    To copy to clipboard, switch view to plain text mode 

    The label appears to be correct ("3-alpha-10.2") in the GUI. The tripple backslashes are necessary so that it becomes \" in the Makefile and then just " when passed to the compiler by the shell.

  12. The following user says thank you to artm for this useful post:

    cgyan1 (6th July 2012)

Similar Threads

  1. Replies: 4
    Last Post: 7th April 2010, 23:09
  2. Why does qmake/moc only process header files?
    By doberkofler in forum Qt Programming
    Replies: 6
    Last Post: 3rd March 2010, 08:32
  3. Get value for variable name defined as string
    By webquinty in forum General Programming
    Replies: 2
    Last Post: 18th November 2009, 10:30
  4. Conflicting header definitions
    By jacky in forum Qt Programming
    Replies: 13
    Last Post: 4th May 2009, 09:51
  5. Replies: 6
    Last Post: 4th April 2006, 07:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.