[cmake-developers] escape double quote in generated file

Kyle Edwards kyle.edwards at kitware.com
Fri Aug 30 10:49:53 EDT 2019


On Fri, 2019-08-30 at 17:36 +0300, Eugene Karpov wrote:
> Hello all,
> 
> I'm working on a cross platform project. On Ubuntu I would like to
> save all the compiler options, definitions and other complier related
> stuff to a generated file to use it later for precompiled header
> generation.
> My issue is that I have to specify a macro that contain double quotes
> for g++ compiler visibility attribute. When I generate a file with
> double quotes they are not escaped. I also tried to use
> `string(replace "\"" "\\\""...)` without effect.
> I've made simple two files project of a shared library to show the
> issue. It has only target compile definitions for simplicity.
> 
> ------------- CMakeLists.txt ------------------
> cmake_minimum_required(VERSION 3.14)
> set(target test)
> project(${target})
> if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
>     set(API_IMPORT_MACRO "__attribute__((visibility(\"default\")))")
>     set(API_EXPORT_MACRO "__attribute__((visibility(\"default\")))")

The quotes here need to be double-escaped, like so:

set(API_IMPORT_MACRO "__attribute__((visibility(\\\"default\\\")))")
set(API_EXPORT_MACRO "__attribute__((visibility(\\\"default\\\")))")

Kyle


More information about the cmake-developers mailing list