Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux CMake get HOSTNAME环境变量_Linux_Bash_Ubuntu_Cmake_Environment Variables - Fatal编程技术网

Linux CMake get HOSTNAME环境变量

Linux CMake get HOSTNAME环境变量,linux,bash,ubuntu,cmake,environment-variables,Linux,Bash,Ubuntu,Cmake,Environment Variables,我将主机名和用户定义为环境变量(Linux Ubuntu) 我使用 message("-- USER environment variable is set to: " $ENV{USER}) message("-- HOSTNAME environment variable is set to: " $ENV{HOSTNAME}) 但检测到的是用户,而不是主机名 输出为 -- USER environment variable is set to: pvicente -- HOSTNAME

我将主机名和用户定义为环境变量(Linux Ubuntu)

我使用

message("-- USER environment variable is set to: " $ENV{USER})
message("-- HOSTNAME environment variable is set to: " $ENV{HOSTNAME})
但检测到的是用户,而不是主机名

输出为

-- USER environment variable is set to: pvicente
-- HOSTNAME environment variable is set to:

注意:我不使用
cmake
,所以我在谷歌上搜索了一下,得出了以下结论。。。有点超出了我的评论范围

根据第一篇帖子@this:

  • sh-c echo\$HOSTNAME
    =>…什么都没有<代码>主机名为 (自动)在
    sh
    shell下设置
  • bash-c echo\$HOSTNAME
    =>glace<代码>主机名是在
    bash
    shell下(自动)设置的
  • cmake
    sh
    shell下运行命令,因此
    HOSTNAME
    将不可用于
    cmake
在同一链接的底部,建议使用
site\u name()
函数

SITE_NAME(mySite)             => obtain hostname and place in variable 'mySite'
MESSAGE(mySite='${mySite}')

。。。这似乎与(稀疏的)文档()不一致。

您可以检查CMake使用哪些环境变量查看

$ cmake -E environment
它在名为
NAME
的环境变量中给出我的主机名。因此,请尝试:

message("-- USER environment variable is set to: " $ENV{USER})
message("-- HOSTNAME environment variable is set to: " $ENV{NAME})
但官方的跨平台CMake方式是:

cmake_host_system_information(RESULT _host_name QUERY HOSTNAME)
message("-- _host_name variable is set to: " ${_host_name})
参考


主机名和用户变量在哪里定义?@MattSchuchard HOSTNAME是一个环境变量…为什么
\u host\u name
而不仅仅是
host\u name
cmake_host_system_information(RESULT _host_name QUERY HOSTNAME)
message("-- _host_name variable is set to: " ${_host_name})