Linux cmake :从脚本设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21047399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cmake : Set environment variables from a script
提问by B?ови?
I have a script that sets all variables needed for the cross-compilation. Here is just part of it :
我有一个脚本来设置交叉编译所需的所有变量。这只是其中的一部分:
export CONFIG_SITE=~/workspace/eldk-5.4/powerpc/site-config-powerpc-linux
export CC="powerpc-linux-gcc -m32 -mhard-float --sysroot=~/workspace/eldk-5.4/powerpc/sysroots/powerpc-linux"
export CXX="powerpc-linux-g++ -m32 -mhard-float --sysroot=~/workspace/eldk-5.4/powerpc/sysroots/powerpc-linux"
export CPP="powerpc-linux-gcc -E -m32 -mhard-float --sysroot=~/workspace/eldk-5.4/powerpc/sysroots/powerpc-linux"
export AS="powerpc-linux-as "
export LD="powerpc-linux-ld --sysroot=~/workspace/eldk-5.4/powerpc/sysroots/powerpc-linux"
export GDB=powerpc-linux-gdb
If I do source environment-setup-powerpc-linux
, all environment variables are imported into the current shell session, and I can compile my example.
如果我这样做source environment-setup-powerpc-linux
,所有环境变量都会导入到当前的 shell 会话中,并且我可以编译我的示例。
Is it possible to import these variables in cmake? If yes, how?
是否可以在 cmake 中导入这些变量?如果是,如何?
A bit more details :
更多细节:
- I am using ELDK v 5.4, and it's install script generates a script which sets all environment variables
- I found this tutorial, which explains how to manually set for cross-compilation, but not how to use the script, which sets everything
- if I call the script before setting cmake, all works fine, and I can cross-compile, but I'd like that cmake calls the script
- 我正在使用ELDK v 5.4,它的安装脚本生成一个脚本来设置所有环境变量
- 我找到了这个教程,它解释了如何手动设置交叉编译,但没有解释如何使用脚本,它设置了所有内容
- 如果我在设置 cmake 之前调用脚本,一切正常,我可以交叉编译,但我希望 cmake 调用脚本
采纳答案by TMS
Reading through the cmake quick start, you can specify variable on a command line:
通过阅读CMake的快速启动,你可以在命令行上指定变量:
cmake -DVARIABLE1=value1 -DVARIABLE2=value2 ...
Otherwise, set
command in the cmake script is probably what you want, see the reference manual. To set the environment variablePATH, do:
否则,set
cmake 脚本中的命令可能就是您想要的,请参阅参考手册。要设置环境变量PATH,请执行以下操作:
set(ENV{PATH} "/home/martink")
To set normal variable, do:
要设置正常变量,请执行以下操作:
set(variable "value")
Not sure which ones you have to set, probably the environment ones.
不确定您必须设置哪些,可能是环境设置。
That said, setting environment variable prior to calling cmakeis often the easiest solution to solve the problem, as in this case: https://stackoverflow.com/a/15053460/684229
也就是说,在调用 cmake 之前设置环境变量通常是解决问题的最简单的解决方案,在这种情况下:https: //stackoverflow.com/a/15053460/684229
回答by Patrick B.
The only way to set a compiler and flags to do cross-compilation reliably with CMake is with a toolchain-file as done in the tutorial you have found.
设置编译器和标志以使用 CMake 可靠地进行交叉编译的唯一方法是使用工具链文件,如您找到的教程中所做的那样。
When we faced the same issue you have (a toolkit which produces a script so set the compile-environment) we changed the toolkit in a way that it produces a toolchain-file along with the script.
当我们遇到您遇到的相同问题时(一个工具包,它生成一个脚本,因此设置编译环境),我们更改了工具包,使其随脚本生成一个工具链文件。
In reality a cmake-toolchain-file does not change that often. The basic flags used for the target are fixed quite early in a project - normally. And with CMake's CMAKE_BUILD_TYPE
you can switch between Debug and Release compilations without changing the toolchain-file.
实际上,cmake-toolchain-file 不会经常更改。用于目标的基本标志在项目中很早就被修复了——通常。使用 CMake,CMAKE_BUILD_TYPE
您可以在 Debug 和 Release 编译之间切换,而无需更改工具链文件。
If you have different targets to support, create different toolchain and use the out-of-source-build with CMake.
如果您有不同的目标要支持,请创建不同的工具链并使用 CMake 的源外构建。
EDIT: One thing you could do is to invoke cmake with the -D-argument setting the variables you want to and having sourced your script before:
编辑:您可以做的一件事是使用 -D-argument 设置您想要的变量并在之前获取脚本来调用 cmake:
source environment-setup-powerpc-linux
cmake -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX etc
The result will be identical as to having used a toolchain-file.
结果将与使用工具链文件相同。