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

huangapple go评论64阅读模式
英文:

How to use value of environment variable in project file?

问题

我正在尝试将一个Homebrew库添加到另一个项目中。

根据另一个答案中的建议(现在无法找到),我在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。

然而,当我尝试构建时,链接时出现错误。

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<XYZ_DIR扩展为什么>

我尝试了每种$$$XYZ_DIR的组合,以及{XYZ_DIR}(XYZ_DIR)。没有一种方法有效,除了上面提到的组合,其他组合都会生成`-L'(没有任何目录)。

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

抱歉,这个问题比预期的要长;但是,我无法想象如何能够合理地缩短它。

英文:

I'm trying to add a homebrew library to another project.

Following a suggestion in another answer that I now cannot find, I have created xyz.prf in mkspecs/features and have added

    config += xyz

to my project file.

xyz.prf contains

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

I've defined XYZ_INC and XYZ_DIR in the build configuration and have run qmake as often as needed.

However, when I try to build, there's an error at link time.

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

Now, looking at the g++ command line, I see -LYZ_DIR which explains why ld can't find libxyz - it ought to be -L$XYZ_DIR or -L&lt;whatever XYZ_DIR expands to&gt;.

I've tried every combination of $ or $$ and XYZ_DIR, {XYZ_DIR} or (XYZ_DIR). None work and all but the above combination end up generating `-L' (without any directory).

What's the right syntax to generate what I need?

Sorry - this question has ended up longer than intended; however, I can't think of a way that it can sensibly be shortened.

答案1

得分: 0

文档所指出,必须使用$$(...)来获取环境变量的内容:

> 变量可用于存储环境变量的内容。这些变量可以在运行qmake时进行评估,或者在生成的Makefile中包含,以便在构建项目时进行评估。

> 要在运行qmake时获取环境值的内容,请使用
> $$(...) 运算符:
>
> DESTDIR = $$(PWD)
> message(项目将安装在$$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命令执行时执行,必须使用$(...),正如文档中所示:

> 要在生成的Makefile处理时获取环境值的内容,请使用$(...)运算符:
>
> DESTDIR = $$(PWD)
> message(项目将安装在$$DESTDIR中)
>
> DESTDIR = $(PWD)
> message(项目将安装在PWD的值中)
> message(当Makefile被处理时。)

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
英文:

As the docs points out, $$(...) must be used to obtain the environment variables:

>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

With the previous code element it is applied only when qmake is executed, but if you want it to be executed with the make command you must use $(...) as indicated by the docs:

> 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

huangapple
  • 本文由 发表于 2020年1月4日 00:47:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定