<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Aug 31, 2019 at 12:36 AM Eugene Karpov <<a href="mailto:karpov.en@gmail.com">karpov.en@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello all,<div><br></div><div>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.</div><div>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.</div><div>I've made simple two files project of a shared library to show the issue. It has only target compile definitions for simplicity.</div><div><br></div><div>------------- CMakeLists.txt ------------------</div><div>cmake_minimum_required(VERSION 3.14)<br>set(target test)<br>project(${target})<br>if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")<br>    set(API_IMPORT_MACRO "__attribute__((visibility(\"default\")))")<br>    set(API_EXPORT_MACRO "__attribute__((visibility(\"default\")))")<br>else()<br>    set(API_IMPORT_MACRO "__declspec(dllimport)")<br>    set(API_EXPORT_MACRO "__declspec(dllexport)")<br>endif()<br>function(export_all_flags _target _filename)<br>  set(_compile_definitions "$<TARGET_PROPERTY:${_target},COMPILE_DEFINITIONS>")<br>  set(_compile_definitions "$<$<BOOL:${_compile_definitions}>:-D$<JOIN:${_compile_definitions},\n-D>\n>")<br>  file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}")<br>endfunction()<br>add_library(${target} SHARED test.cpp)<br>target_compile_definitions(${target}<br>    PRIVATE API=${API_EXPORT_MACRO}<br>    INTERFACE API=${API_IMPORT_MACRO})<br>export_all_flags(${target} ${CMAKE_BINARY_DIR}/flags.txt)<br></div><div>------------- CMakeLists.txt ------------------<br></div><div>------------- test.cpp ------------------<br></div><div>void API test() {}<br></div><div>------------- test.cpp ------------------<br></div><div><br></div><div>The result file "flags.txt" is following:</div><div>-DAPI=__attribute__((visibility("default")))<br></div><div><br></div><div>I would like any solution to make the result as:</div><div>-DAPI=__attribute__((visibility(\"default\")))</div></div></blockquote><div><br></div><div>Are you free to modify the headers where the API_IMPORT/API_EXPORT symbols are used? If so, then it is far easier to delegate the definition of such a symbol to a separate header file rather than try to define it directly on the compiler command line. The <a href="https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html">GenerateExportHeader</a> module provides a convenient way to create such a header and also set the relevant details in your CMake target to do the right thing when building and when consuming the library. I'd expect it to be suitable for a precompiled header scenario too.</div><div><br></div><div>And shameless plug, if you're interested in this area (symbol visibility), part of my <a href="https://sched.co/SfnH">upcoming CppCon talk</a> in a few weeks will cover exactly this topic. ;)</div><div><br></div></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com" target="_blank">https://crascit.com</a><br></div><div><br></div><div>Get the hand-book for every CMake user: <a href="https://crascit.com/professional-cmake/" target="_blank">Professional CMake: A Practical Guide</a><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div>