Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add quotation mark in CMake string

Tags:

escaping

cmake

I am using CMake to create and build my project solution. i am using the following command to add a post build event to copy a .tlb from the local bin to the program bin.

ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD COMMAND xcopy /D /Y "${CMAKE_SOURCE_DIR}LocalBin\\example.tlb" "${CMAKE_SOURCE_DIR}ProgramBin\\$<CONFIGURATION>\\")

When this adds the command to the project properties , it is added as

xcopy /D /Y LocalBin\example.tlb ProgramBin\Debug\

However this gives me an error. Exited with Code 4.

If i go into the project properties and hack the command line and change it to add " "

xcopy /D /Y "LocalBin\example.tlb " "ProgramBin\Debug\"

It works.

Is there a way i can change the CMake add custom command to include the " " in the actual command line so it will work and there is no need to manually change the project properties.

like image 900
PowPowPowell Avatar asked Jan 13 '16 10:01

PowPowPowell


2 Answers

You have to escape the quotation mark inside the string. Escaping is done in CMake with a back slash. So adding a quotation mark to your string, add \".

Quoting the CMake documentation https://cmake.org/cmake/help/v3.4/manual/cmake-language.7.html#escape-sequences

A \ followed by a non-alphanumeric character simply encodes the literal character without interpreting it as syntax

In your case you'll end up with

"\"${CMAKE_SOURCE_DIR}LocalBin\\example.tlb\""

instead of

"${CMAKE_SOURCE_DIR}LocalBin\\example.tlb"
like image 178
usr1234567 Avatar answered Sep 22 '22 17:09

usr1234567


After struggling with this for some time, I found a workable solution using generator expressions:

ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD COMMAND xcopy /D /Y $<1:"${CMAKE_SOURCE_DIR}LocalBin\\example.tlb"> $<1:"${CMAKE_SOURCE_DIR}ProgramBin\\$<CONFIGURATION>\\">)

This is an open bug in CMake.

like image 37
Alex Goldberg Avatar answered Sep 23 '22 17:09

Alex Goldberg