cmake:环境变量(environment variable)读写和if判断

本文介绍了如何在CMake中读写环境变量。读取环境变量时需使用$ENV{变量名}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cmake中对环境变量读写都是通过ENV前缀来访问环境变量
读取环境变量则要使用 $ENV{JAVA_HOME}这样的格式
写环境变量如下:

set( ENV{PATH} /home/martink )

if语句判断环境变量是否定义要用下面的格式

if(NOT DEFINED ENV{JAVA_HOME})
	# 没有找到JAVA_HOME环境变量
	message(FATAL_ERROR "not defined environment variable:JAVA_HOME")  
endif()
#不能用if(ENV{JAVA_HOME})形式来判断是否定义 
#但可以用if($ENV{JAVA_HOME})

总结一下,就可以看出来,读取环境变量时要在ENV前加$符号,而写和if判断是否定义时,ENV{JAVA_HOME}指代变量名所以不加$符号。

px4_user@DESKTOP-807L394:~$ sudo apt install code Reading package lists... Done Building dependency tree... Done Reading state information... Done No apt package "code", but there is a snap with that name. Try "snap install code" E: Unable to locate package code px4_user@DESKTOP-807L394:~$ ls\ > ^C px4_user@DESKTOP-807L394:~$ ^C px4_user@DESKTOP-807L394:~$ ls PX4-Autopilot ubuntu-wsl2-systemd-script px4_user@DESKTOP-807L394:~$ cd PX4-Autopilot/ px4_user@DESKTOP-807L394:~/PX4-Autopilot$ ls CMakeLists.txt Jenkinsfile Tools install_geographiclib_datasets.sh posix-configs CODE_OF_CONDUCT.md Kconfig appveyor.yml integrationtests src CONTRIBUTING.md LICENSE boards launch test CTestConfig.cmake Makefile cmake msg test_data Documentation README.md eclipse.cproject package.xml validation Firmware.sublime-project ROMFS eclipse.project platforms px4_user@DESKTOP-807L394:~/PX4-Autopilot$ code . Command 'code' not found, but can be installed with: sudo snap install code px4_user@DESKTOP-807L394:~/PX4-Autopilot$ sudo snap install code error: This revision of snap "code" was published using classic confinement and thus may perform arbitrary system changes outside of the security sandbox that snaps are usually confined to, which may put your system at risk. If you understand and want to proceed repeat the command including --classic. px4_user@DESKTOP-807L394:~/PX4-Autopilot$ sudo snap install code --classic 2025-07-09T17:36:53+08:00 INFO Waiting for automatic snapd restart... code 2901c5ac from Visual Studio Code (vscode✓) installed px4_user@DESKTOP-807L394:~/PX4-Autopilot$ code . mkdir: cannot create directory ‘’: No such file or directory To use Visual Studio Code with the Windows Subsystem for Linux, please install Visual Studio Code in Windows and uninstall the Linux version in WSL. You can then use the `code` command in a WSL terminal just as you would in a normal command prompt. Do you want to continue anyway? [y/N] y To no longer see this prompt, start Visual Studio Code with the environment variable DONT_PROMPT_WSL_INSTALL defined. px4_user@DESKTOP-807L394:~/PX4-Autopilot$ code . mkdir: cannot create directory ‘’: No such file or directory To use Visual Studio Code with the Windows Subsystem for Linux, please install Visual Studio Code in Windows and uninstall the Linux version in WSL. You can then use the `code` command in a WSL terminal just as you would in a normal command prompt. Do you want to continue anyway? [y/N] y To no longer see this prompt, start Visual Studio Code with the environment variable DONT_PROMPT_WSL_INSTALL defined. px4_user@DESKTOP-807L394:~/PX4-Autopilot$ code . mkdir: cannot create directory ‘’: No such file or directory To use Visual Studio Code with the Windows Subsystem for Linux, please install Visual Studio Code in Windows and uninstall the Linux version in WSL. You can then use the `code` command in a WSL terminal just as you would in a normal command prompt. Do you want to continue anyway? [y/N] y To no longer see this prompt, start Visual Studio Code with the environment variable DONT_PROMPT_WSL_INSTALL defined. 为什么打不开code
最新发布
07-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值