在*.pro文件之间传递变量

azf*_*azf 10 qt qmake global-variables

我有以下*.pro文件:

领导解决方案的人

# head - pro file :
TEMPLATE = subdirs
CONFIG = qt thread ordered

# save root directory
PROJECT_ROOT_DIRECTORY = $$_PRO_FILE_PWD_
message("Master pro file path : ["$${PROJECT_ROOT_DIRECTORY}"]") // output : "Master pro file path : [/Path/To/Directory]"

# project subdirs
SUBDIRS += PROJECT1
Run Code Online (Sandbox Code Playgroud)

# PROJECT1 - pro file :
TEMPLATE = app

# etc.

# output 'PROJECT_ROOT_DIRECTORY ' contents
message("Master pro file path : "$${PROJECT_ROOT_DIRECTORY}) // output :  "Master pro file path : []"
Run Code Online (Sandbox Code Playgroud)

如何在2个pro文件之间传递变量(这里是变量PROJECT_ROOT_DIRECTORY)?

编辑:

这是因为这个同样的问题这一个,但我没有看到"怎么另一个选项是 "的回答可以帮助我.

lee*_*mes 8

您可以将变量定义放在一个.pri文件中,然后将其包含在所需的所有.pro文件中.请注意,您需要告诉.pro子目录中的.pri文件找到文件的路径.

head.pro:

# head - pro file :
TEMPLATE = subdirs
CONFIG = qt thread ordered

# configuration
include(config.pri)
message("Master pro file path : ["$${PROJECT_ROOT_DIRECTORY}"]") # output : "Master pro file path : [/Path/To/Directory]"

# project subdirs
SUBDIRS += PROJECT1
Run Code Online (Sandbox Code Playgroud)

config.pri:

# save root directory
PROJECT_ROOT_DIRECTORY = $$PWD // not $$_PRO_FILE_PWD_!
Run Code Online (Sandbox Code Playgroud)

PROJECT1/project1.pro:

# PROJECT1 - pro file :
TEMPLATE = app

# configuration
include(../config.pri)  # note that you need to put "../" before the .pri path
message("Master pro file path : ["$${PROJECT_ROOT_DIRECTORY}"]") # should now output :  "Master pro file path : [/Path/To/Directory]"
Run Code Online (Sandbox Code Playgroud)

  • https://wiki.qt.io/QMake-top-level-srcdir-and-builddir,这可能是在qt-5之后在.qmake.conf中定义变量的解决方案。 (2认同)