Results 1 to 8 of 8

Thread: Shared/Global/Inherited Variables between projects

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Shared/Global/Inherited Variables between projects

    Hi,
    My question is, if there is a way to declare a variable in a PRO file, so it is visible in all subproject?
    I have a project that has different build options, where different flags to the compiler are passed. I want to have a base PRO file for each of the different builds and in these PRO files to define the different flags.
    The horrible alternative is to create different versions of every subproject.
    I am also aware that I can pass to QMake a variable that defines which build to execute, but in Qt Creator those are kept in the .user file, which is not under version control and is not share amongst developers.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Shared/Global/Inherited Variables between projects

    The way I do this is that I put the parts common to all subprojects in a .pri file, say common.pri, which I include from each subproject's .pro file using include(path/to/common.pri) (say include(../common.pri) if your subprojects are in subdirectories of the directory containing common.pri).

    Then you can set global flags in common.pri that will affect the build process in each subproject (e.g. debug vs release, custom CONFIG variables, etc.).

  3. #3
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Shared/Global/Inherited Variables between projects

    Quote Originally Posted by yeye_olive View Post
    The way I do this is that I put the parts common to all subprojects in a .pri file, say common.pri, which I include from each subproject's .pro file using include(path/to/common.pri) (say include(../common.pri) if your subprojects are in subdirectories of the directory containing common.pri).

    Then you can set global flags in common.pri that will affect the build process in each subproject (e.g. debug vs release, custom CONFIG variables, etc.).
    Thank you for your reply, but in this scenario how are you going to choose between, let's say, build_option_1, build_option_2 and build_option_3?

  4. #4
    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: Shared/Global/Inherited Variables between projects

    You add CONFIG += build_option_1 (or equivalent) to the project file you want to build with option 1.
    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.


  5. #5
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Shared/Global/Inherited Variables between projects

    Hi

    I have the following *.pro files :

    Qt Code:
    1. # head - pro file :
    2. TEMPLATE = subdirs
    3. CONFIG = qt thread ordered
    4.  
    5. # save root directory
    6. PROJECT_ROOT_DIRECTORY = $$_PRO_FILE_PWD_
    7. message("Master pro file path : ["$${PROJECT_ROOT_DIRECTORY}"]") // output : "Master pro file path : [/Path/To/Directory]"
    8.  
    9. # project subdirs
    10. SUBDIRS += PROJECT1
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. # PROJECT1 - pro file :
    2. TEMPLATE = app
    3.  
    4. # etc.
    5.  
    6. # output 'PROJECT_ROOT_DIRECTORY ' contents
    7. message("Master pro file path : "$${PROJECT_ROOT_DIRECTORY}) // output : "Master pro file path : []"
    To copy to clipboard, switch view to plain text mode 

    How do I pass variables between the 2 pro files (here the variable is PROJECT_ROOT_DIRECTORY) ?

  6. #6

    Default Re: Shared/Global/Inherited Variables between projects

    How did you solve it in the end?

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Shared/Global/Inherited Variables between projects

    Quote Originally Posted by Marco Medri View Post
    How did you solve it in the end?
    Using an include() file...
    Qt Code:
    1. # master.pro
    2. TEMPLATE = subdirs
    3. CONFIG += ordered
    4.  
    5. include(master.pri)
    6. message("Master PROJECT_ROOT_DIRECTORY: "$${PROJECT_ROOT_DIRECTORY})
    7.  
    8. # project subdirs
    9. SUBDIRS += project1
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. # master.pri
    2. PROJECT_ROOT_DIRECTORY = $${PWD}
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. # project1/project1.pro
    2. include(../master.pri)
    3.  
    4. TEMPLATE = app
    5. TARGET = project1
    6. INCLUDEPATH += .
    7.  
    8. # Input
    9. SOURCES += main.cpp
    10.  
    11. # output 'PROJECT_ROOT_DIRECTORY ' contents
    12. message("$${TARGET} PROJECT_ROOT_DIRECTORY: "$${PROJECT_ROOT_DIRECTORY})
    To copy to clipboard, switch view to plain text mode 
    Output:
    Qt Code:
    1. chrisw@newton /tmp/master $ qmake
    2. Info: creating stash file /tmp/master/.qmake.stash
    3. Project MESSAGE: Master PROJECT_ROOT_DIRECTORY: /tmp/master
    4.  
    5. chrisw@newton /tmp/master $ make
    6. cd project1/ && ( test -e Makefile || /usr/lib64/qt5/bin/qmake -o Makefile /tmp/master/project1/project1.pro ) && make -f Makefile
    7. Project MESSAGE: project1 PROJECT_ROOT_DIRECTORY: /tmp/master
    8. make[1]: Entering directory '/tmp/master/project1'
    9. ...
    10. make[1]: Leaving directory '/tmp/master/project1'
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 28th January 2019 at 06:10.

  8. #8
    Join Date
    Jan 2020
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Shared/Global/Inherited Variables between projects

    Hi,
    I also need to access a qmake variable from some subdirs projects, but in my case I need access to the OUT_PWD of master.pro.
    The solution given by @ChrisW67 does not work in this case (the OUT_PWD of project1 is printed).
    I need to access to the value of OUT_PWD computed in master.pro inside project1.
    Is there any way to have access to this value from a subdir project?

Similar Threads

  1. QDevelop - global variables & help
    By impeteperry in forum Qt-based Software
    Replies: 2
    Last Post: 10th June 2011, 00:28
  2. Using Singletton Pattern for global variables
    By harmodrew in forum Newbie
    Replies: 6
    Last Post: 7th August 2010, 10:37
  3. Qt and global variables
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 2nd February 2007, 00:42
  4. Global variables
    By Mariane in forum Newbie
    Replies: 14
    Last Post: 10th October 2006, 18:23
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 20:56

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.