<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">Am 01.11.19 um 17:24 schrieb Stephen
      Morris:<br>
    </div>
    <blockquote type="cite"
cite="mid:VE1PR06MB6350EC60CCAE32D7386D1815C9620@VE1PR06MB6350.eurprd06.prod.outlook.com">
      <pre class="moz-quote-pre" wrap="">On 1 November at 10:02, Stephen Morris wrote:

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">My approach is basically to set up a custom command thus:
set(CXX_FLAGS ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG})   .. or whatever, depending on the current configuration..
get_target_property(compile_options, mytarget, COMPILE_OPTIONS) add_custom_command(OUTPUT myheader.h.gch
                                         COMMAND ${CMAKE_CXX_COMPILER} ${CXX_FLAGS} ${compile_options} -fPIC -std=gnu++17 -c myheader.h -o myheader.h.gch
                                          DEPENDS myheader.h) add_custom_target(BuildMyPCH
                                    DEPENDS myheader.h.gch) add_dependencies(mytarget, BuildMyPCH)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
My earlier question still stands, but at the time I wrote it I had only tested it for Debug builds where ${CMAKE_CXX_FLAGS_DEBUG} consisted of a single item, '-g'.

I've since tried doing the same thing for a Release build, and this failed because ${CMAKE_CXX_FLAGS_RELEASE} has two items, '-O3 -DNDEBUG'. When the custom command is executed, this somehow becomes "-O3\ -DNDEBUG" on the command line, and the presence of the stray backslash causes the compilation to fail with the message,

"cc1plus: error: argument to '-O' should be a non-negative integer. 'g', 's' or 'fast'"

So why isn't the cmake variable placed properly onto the command line, and what can I do to prevent this behaviour?</pre>
    </blockquote>
    <p>Welcome to the wonderful world of lists and their interaction
      with quoting in CMake ! Since space is used as a list item
      separator outside of double quoted strings, CMake is escaping the
      space within CXX_FLAGS to keep it as a single element within the
      list of arguments passed to add_custom_command. To avoid this, you
      need to turn CXX_FLAGS into a list prior to using it in
      add_custom_command:</p>
    <p>string(REGEX REPLACE " " ";" CXX_FLAGS "${CXX_FLAGS}")<br>
    </p>
    <p>Of course, this will not work if you want to pass a double quoted
      string as an actual command line argument ...<br>
    </p>
    <div class="moz-signature">-- <br>
      <div style="width:480px; text-align: left; font-family: Arial,
        Helvetica, sans-serif;">
        <p style="color: #062d64; font-size: 14px; text-align: left;
          font-family: Arial, Helvetica, sans-serif;">
          <b>Dr. Eric Dönges</b>
          <br>
          Senior Software Engineer
        </p>
        <p style="color: #062d64; font-size: 12px; text-align: left;
          font-family: Arial, Helvetica, sans-serif;">MVTec Software
          GmbH | Arnulfstr. 205 | 80634 Munich | Germany
          <br>
          <a style="font-size: 12px; font-family: Arial, Helvetica,
            sans-serif; color: #062d64;"
            href="mailto:mustermann@mvtec.com">doenges@mvtec.com</a> |
          Tel: +49 89 457 695-0 | <a style="font-size: 12px;
            font-family: Arial, Helvetica, sans-serif; color: #062d64;"
            href="http://www.mvtec.com">www.mvtec.com</a>
          <br>
        </p>
        <p>
          <span style="font-size: 12px; font-family: Arial, Helvetica,
            sans-serif; color: #062d64;">Find our privacy policy <a
              style="font-size: 12px; font-family: Arial, Helvetica,
              sans-serif; color: #062d64;"
              href="https://www.mvtec.com/imprint">here</a>.</span> </p>
        <p style="color: #062d64; font-size: 12px; text-align: left;
          font-family: Arial, Helvetica, sans-serif;"><img
src="https://mvtec.com/fileadmin/Redaktion/newsletter/mail-signature/newsletter-icon.png"
            valign="bottom" width="16" height="16"> <a style="font-size:
            12px; font-family: Arial, Helvetica, sans-serif; color:
            #062d64; font-weight: bold;"
            href="https://www.mvtec.com/newsletter">Sign up</a> for our
          MVTec Newsletter!</p>
        <p style="margin: 0px; color: #666; font-size: 12px; text-align:
          left; font-family: Arial, Helvetica, sans-serif;">Geschäftsführer:
          Dr. Wolfgang Eckstein, Dr. Olaf Munkelt<br>
          Amtsgericht München HRB 114695</p>
        <p style="margin: 0px; color: #666; font-size: 12px; text-align:
          left; font-family: Arial, Helvetica, sans-serif;"> </p>
        <img
src="https://www.mvtec.com/fileadmin/Redaktion/newsletter/mail-signature/mvtec-logo-line.png"
          alt="MVTec Software GmbH Logo"></div>
    </div>
  </body>
</html>