Is there a built-in function to convert a string to a C string literal. For example:
set(foo [[Hello\ World"!\]])
convert_to_cstring_literal(bar "${foo}")
message("${foo}") # Should print (including quotes): "Hello\\ World\"!\\"
I mean I can do this with considerable effort with regexes, but if there's a built-in function it would be a lot nicer.
Turning my comment into an answer
Slightly modifying the CMake's function _cpack_escape_for_cmake from CPack.cmake I was able to successfully test the following:
cmake_minimum_required(VERSION 2.8)
project(CStringLiteral)
function(convert_to_cstring_literal var value)
string(REGEX REPLACE "([\\\$\"])" "\\\\\\1" escaped "${value}")
set("${var}" "\"${escaped}\"" PARENT_SCOPE)
endfunction()
set(foo [[Hello\ World"!\]])
convert_to_cstring_literal(bar "${foo}")
message("${bar}") # prints "Hello\\ World\"!\\"
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