如何在项目文件中使用环境变量的值?

How to use value of environment variable in project file?

我正在尝试将自制程序库添加到另一个项目。

根据我现在找不到的另一个答案中的建议,我在 mkspecs/features 中创建了 xyz.prf 并添加了

    config += xyz

到我的项目文件。

xyz.prf 包含

INCLUDEPATH += $XYZ_INC
DEPENDPATH += $XYZ_INC

win32:CONFIG(release, debug|release): LIBS += -L$XYZ_DIR -llibxyz
else:win32:CONFIG(debug, debug|release): LIBS += -L$XYZ_DIR -llibxyz
else:unix: LIBS += -L$XYZ_DIR -llibxyz

我已经在构建配置中定义了 XYZ_INC 和 XYZ_DIR,并根据需要经常使用 运行 qmake。

但是,当我尝试构建时,在 link 时出现错误。

g++ -Wl,-rpath,/opt/Qt/5.12.5/gcc_64/lib -o someprogram someprogram.o   -L/home/alan/work/myStuff/sqlPrettyPrinter/v3/build-SQLPPv3-Desktop_Qt_5_12_5_GCC_64bit-Debug/test/test_collationname/../../libsqlpp/ -llibsqlpp -LYZ_DIR -llibxyz /opt/Qt/5.12.5/gcc_64/lib/libQt5Test.so /opt/Qt/5.12.5/gcc_64/lib/libQt5Core.so -lpthread   
/usr/bin/ld: cannot find -llibxyz

现在,查看 g++ 命令行,我看到 -LYZ_DIR 这解释了为什么 ld 找不到 libxyz - 它应该是 -L$XYZ_DIR-L<whatever XYZ_DIR expands to> .

我已经尝试了 $$$XYZ_DIR{XYZ_DIR}(XYZ_DIR) 的所有组合。 None 工作,除了上述组合之外的所有组合最终生成 `-L'(没有任何目录)。

生成我需要的内容的正确语法是什么?

抱歉 - 这个问题比预期的要长;但是,我想不出一种可以合理缩短它的方法。

正如the docs指出的,必须使用$$(...)来获取环境变量:

Variables can be used to store the contents of environment variables. These can be evaluated at the time when qmake is run, or included in the generated Makefile for evaluation when the project is built.

To obtain the contents of an environment value when qmake is run, use the $$(...) operator:

DESTDIR = $$(PWD)
message(The project will be installed in $$DESTDIR)

xyz.prf

INCLUDEPATH += $$(XYZ_INC)
DEPENDPATH += $$(XYZ_INC)

win32:CONFIG(release, debug|release): LIBS += -L$$(XYZ_DIR) -llibxyz
else:win32:CONFIG(debug, debug|release): LIBS += -L$$(XYZ_DIR) -llibxyz
else:unix: LIBS += -L$$(XYZ_DIR) -llibxyz

对于前面的代码元素,它仅在执行 qmake 时应用,但如果您希望使用 make 命令执行它,则必须使用 $(...),如文档所示:

To obtain the contents of an environment value at the time when the generated Makefile is processed, use the $(...) operator:

DESTDIR = $$(PWD)
message(The project will be installed in $$DESTDIR)

DESTDIR = $(PWD)
message(The project will be installed in the value of PWD)
message(when the Makefile is processed.)

xyz.prf

INCLUDEPATH += $(XYZ_INC)
DEPENDPATH += $(XYZ_INC)

win32:CONFIG(release, debug|release): LIBS += -L$(XYZ_DIR) -llibxyz
else:win32:CONFIG(debug, debug|release): LIBS += -L$(XYZ_DIR) -llibxyz
else:unix: LIBS += -L$(XYZ_DIR) -llibxyz