Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake get HOSTNAME environment variable

I have both HOSTNAME and USER defined as environment variables (Linux Ubuntu)

echo $USER
pvicente
echo $HOSTNAME
glace

I get the variables in CMakeLists.txt using

message("-- USER environment variable is set to: " $ENV{USER})
message("-- HOSTNAME environment variable is set to: " $ENV{HOSTNAME})

but USER is detected and not HOSTNAME

output is

-- USER environment variable is set to: pvicente
-- HOSTNAME environment variable is set to:
like image 394
Pedro Vicente Avatar asked Jul 17 '26 10:07

Pedro Vicente


2 Answers

You could check which environment variables CMake does see with

$ cmake -E environment

It gives my hostname in an environment variable named NAME. So try:

message("-- USER environment variable is set to: " $ENV{USER})
message("-- HOSTNAME environment variable is set to: " $ENV{NAME})

But the official cross-platform CMake way is:

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

Reference

  • cmake_host_system_information()
like image 168
Florian Avatar answered Jul 19 '26 07:07

Florian


NOTE: I don't work with cmake so I did a bit of googling and came up with the following ... a bit more than I could fit into a comment ...

According to the first post @ this link:

  • sh -c echo \$HOSTNAME => ...nothing... ; HOSTNAME is not (automatically) set under the sh shell
  • bash -c echo \$HOSTNAME => glace ; HOSTNAME is (automatically) set under the bash shell
  • cmake runs commands under the sh shell, so HOSTNAME will not be available to cmake

At the bottom of that same link there's a recommendation to use the site_name() function ...

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

... which appears to jive with the (sparse) documentation (site_name() documentation)

like image 41
markp-fuso Avatar answered Jul 19 '26 06:07

markp-fuso