powershellgcccmakeenvironment-variablesescaping

how can i properly include a path with spaces in cmake on windows powershell and augment it with variable or command to pass to gcc?


the only thing that works is manually putting the entire path with single qoutes '. like

cmake -G Ninja -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_FLAGS='-IC:/Program" "Files" "(x86)/Windows" "Kits/10/Include/x/um' -DCMAKE_CXX_STANDARD=20 -DCMAKE_CXX_STANDARD_REQUIRED=ON -DCMAKE_BUILD_TYPE=Release -S ..

where it is all encased in ' and the spaces are encased in " which is the only way it works but the problem is the x directory changes so i had $((ls 'C:/Program Files (x86)/Windows Kits/10/Include')[0].name) in place of x to get the first available version but i cant call commands or variables inside '.

DCMAKE_CXX_FLAGS='-IC:/Program" "Files" "(x86)/Windows" "Kits/10/Include/x/um' is the part I'm talking about

I tried \ before spaces, tried in casing ' before and after the spaces, using " to encase the entire thing somehow escapes the spaces still as well, same with \ before the spaces, only encasing the spaces with " they all break the path resulting in the same errors:

g++.exe: warning: Files: linker input file unused because linking not done
g++.exe: error: Files: linker input file not found: No such file or directory
g++.exe: warning: (x86)/Windows: linker input file unused because linking not done
g++.exe: error: (x86)/Windows: linker input file not found: No such file or directory
g++.exe: warning: Kits/10/Include: linker input file unused because linking not done
g++.exe: error: Kits/10/Include: linker input file not found: No such file or directory

I tried other things like putting the path in a variable before but it breaks also, i tried segmenting the variables into things like $f="'" and $r='"' as part of the final variable or straight in the cmake command but still the same result. Is it really impossible to get the directory dynamically and I have to hard code it here? I tried in a similar thing in a linux bash shell with spaces and variable in the same cmake flag to include something and it had no issues so i am so confused as to why this is happening and cant find a way around it.


Solution

  • In Powershell, normal quotation marks prevent the shell from breaking the string into separate words, and escaped quotation marks are treated as literal characters. The backtick is the escape character.

    These should work:

        -DCMAKE_CXX_FLAGS="`"/your path/with spaces`""
        -DCMAKE_CXX_FLAGS=`"$((your command)[0].name)`"
        -DCMAKE_CXX_FLAGS="`"$((your command)[0].name) and some thing/ with spaces`""