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:
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()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 shellbash -c echo \$HOSTNAME => glace ; HOSTNAME is (automatically) set under the bash shellcmake runs commands under the sh shell, so HOSTNAME will not be available to cmakeAt 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With