CMake environment variable names with special characters

Most environment variable have alphanumeric names and don’t need any special consideration to access. On Windows, some important programs still use the “Program Files (x86)” directory, denoted by environment variable “ProgramFiles(x86)”.

cmake_minimum_required(VERSION 3.10)

set(px $ENV{ProgramFiles\(x86\)})

message(STATUS "${px}")

Most other code languages don’t have any particular issues using environment variables named with special characters.

All of the following print like:

C:\Program Files (x86)

Matlab:

getenv("ProgramFiles(x86)")

Python:

python -c "import os; print(os.environ['ProgramFiles(x86)'])"

C++:

#include <cstdlib>
#include <iostream>

int main()
{
    std::cout << std::getenv("ProgramFiles(x86)") << "\n";
    return EXIT_SUCCESS;
}

Fortran:

program env

implicit none

integer :: i
character(100) :: path

call get_environment_variable('ProgramFiles(x86)', path, status=i)
if (i/=0) error stop "env var ProgramFiles(x86) not found"

print '(a)', path

end program env