naichuans
naichuans

Reputation: 41

cmake error : invalid escape sequence \\c

In order to make a symbol link, I run a command using cmake install. my code as below:

file(TO_NATIVE_PATH "${CMAKE_CURRENT_LIST_DIR}/alib.pyd" native_pyd)
file(TO_NATIVE_PATH "${DESTINATION}/lib/alib.pyd" release_pyd)
install(CODE "execute_process(COMMAND cmd /c del /f /s /q ${native_pyd} & mklink ${native_pyd}  \"${release_pyd}\"
                              COMMAND_ECHO STDOUT)")

Then I get an error: "invalid escape sequence \c"
${native_pyd} string is "...\code...", so it cause the warning above.
How to fix it gracefully?

Upvotes: 1

Views: 703

Answers (1)

vre
vre

Reputation: 6744

You need to escape all the path arguments to your commands. This can be either Windows specific as you have already done and using mklink. To relieve yourself of the need to escape all quotes inside the CODE fragment you can use the bracket argument syntax. Then the command would be:

file(TO_NATIVE_PATH "${CMAKE_CURRENT_LIST_DIR}/alib.pyd" native_pyd)
file(TO_NATIVE_PATH "${DESTINATION}/lib/alib.pyd" release_pyd)
install(CODE [=[
  execute_process(
    COMMAND cmd /c del /f /s /q "${native_pyd}" & mklink "${native_pyd}" "${release_pyd}"
    COMMAND_ECHO STDOUT
  )
]=])

Another solution that is platform independent and uses the CMake builtin command line tools can be formulated as follows (for a CMake version >= 3.13):

install(CODE [=[
  execute_process(COMMAND "${CMAKE_COMMAND}" -E remove_directory "${native_pyd}")
  execute_process(COMMAND "${CMAKE_COMMAND}" -E create_symlink "${release_pyd}" "${native_pyd}")
]=])

Upvotes: 1

Related Questions