<div dir="ltr">Dear Oleksii,<div>indeed it works! Thank you! I have opened an issue on the CMake repository: <a href="https://gitlab.kitware.com/cmake/cmake/issues/17501">https://gitlab.kitware.com/cmake/cmake/issues/17501</a></div><div>I think it could still be a bug, since target_compile_definitions and target_link_libraries seem to evaluate the expression differently.</div><div>  All the best,</div><div>   Roberto </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 22, 2017 at 11:40 AM, Oleksii Vilchanskyi <span dir="ltr"><<a href="mailto:oleksii.vilchanskyi@gmail.com" target="_blank">oleksii.vilchanskyi@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">UPD. forgot to CC the mailing list<br>
<span class="im HOEnZb"><br>
Hi,<br>
<br>
I am also confused by this and decided to test it. I found a long thread<br>
from 2012<br>
<<a href="https://cmake.org/pipermail/cmake-developers/2012-August/thread.html#16509" rel="noreferrer" target="_blank">https://cmake.org/pipermail/<wbr>cmake-developers/2012-August/<wbr>thread.html#16509</a>><br>
that discusses list expansion issues. It doesn't tell though whether the<br>
patch was mainlined.<br>
<br>
tldr; if you quote the whole genexpr, the list will expand properly.<br>
Below is the working code.<br>
<br>
------------------------------<wbr>------------------------------<wbr>----------<br>
</span><span class="im HOEnZb">cmake_minimum_required(VERSION 3.3 FATAL_ERROR)<br>
<br>
project(recipe-09 CXX)<br>
<br>
set(CMAKE_CXX_STANDARD 11)<br>
set(CMAKE_CXX_EXTENSIONS OFF)<br>
set(CMAKE_CXX_STANDARD_<wbr>REQUIRED ON)<br>
<br>
list(APPEND CXX_BASIC_FLAGS "-g3" "-O1")<br>
<br>
include(CheckCXXCompilerFlag)<br>
<br>
set(ASAN_FLAGS "-fsanitize=address -fno-omit-frame-pointer")<br>
set(CMAKE_REQUIRED_FLAGS ${ASAN_FLAGS})<br>
check_cxx_compiler_flag(${<wbr>ASAN_FLAGS} asan_works)<br>
unset(CMAKE_REQUIRED_FLAGS)<br>
<br>
add_executable(asan-example asan-example.cpp)<br>
string(REPLACE " " ";" _asan_flags ${ASAN_FLAGS})<br>
target_compile_options(asan-<wbr>example<br>
  PUBLIC<br>
    ${CXX_BASIC_FLAGS}<br>
    $<$<BOOL:${asan_works}>:${_<wbr>asan_flags}><br>
  )<br>
target_link_libraries(asan-<wbr>example<br>
  PUBLIC<br>
</span><span class="im HOEnZb">    "$<$<BOOL:${asan_works}>:${_<wbr>asan_flags}>" # <-- NOTE THIS<br>
  )<br>
------------------------------<wbr>------------------------------<wbr>----------<br>
<br>
On 19.11.2017 19:16, Roberto Di Remigio wrote:<br>
</span><div class="HOEnZb"><div class="h5">> Hello,<br>
> I am trying to compile the following code snippet:<br>
><br>
> int main(int argc, char ** argv) {<br>
>   int stack_array[100];<br>
>   stack_array[1] = 0;<br>
>   return stack_array[argc + 100]; // BOOM<br>
> }<br>
><br>
> The followin CMakeLists.txt works fine, but you can notice that<br>
> target_compile_options takes a list, while target_link_libraries takes a<br>
> string. Always using the string will result in a compilation error,<br>
> because the compiler flags are passed quoted to the compiler. Always<br>
> using the list doesn't work either, because the generator expression is<br>
> evaluated as $<1:-fsanitize=address -fno-omit-frame-pointer> and then<br>
> passed as-is to the linker, which doesn't know what to do with it.<br>
> Is this supposed to be like this? If yes, what's the rationale?<br>
> Thanks in advance!<br>
>   Roberto<br>
><br>
> cmake_minimum_required(VERSION 3.3 FATAL_ERROR)<br>
><br>
> project(recipe-09 CXX)<br>
><br>
> set(CMAKE_CXX_STANDARD 11)<br>
> set(CMAKE_CXX_EXTENSIONS OFF)<br>
> set(CMAKE_CXX_STANDARD_<wbr>REQUIRED ON)<br>
><br>
> list(APPEND CXX_BASIC_FLAGS "-g3" "-O1")<br>
><br>
> include(CheckCXXCompilerFlag)<br>
><br>
> set(ASAN_FLAGS "-fsanitize=address -fno-omit-frame-pointer")<br>
> set(CMAKE_REQUIRED_FLAGS ${ASAN_FLAGS})<br>
> check_cxx_compiler_flag(${<wbr>ASAN_FLAGS} asan_works)<br>
> unset(CMAKE_REQUIRED_FLAGS)<br>
><br>
> add_executable(asan-example asan-example.cpp)<br>
> string(REPLACE " " ";" _asan_flags ${ASAN_FLAGS})<br>
> target_compile_options(asan-<wbr>example<br>
>   PUBLIC<br>
>     ${CXX_BASIC_FLAGS}<br>
>     $<$<BOOL:${asan_works}>:${_<wbr>asan_flags}><br>
>   )<br>
> target_link_libraries(asan-<wbr>example<br>
>   PUBLIC<br>
>     $<$<BOOL:${asan_works}>:${<wbr>ASAN_FLAGS}><br>
>   )<br>
><br>
><br>
<br>
</div></div><div class="HOEnZb"><div class="h5">--<br>
Regards,<br>
Oleksii Vilchanskyi<br>
PGP:<wbr>0x8D3A0E046BDE941F2A53867CE3FD<wbr>952D48C0B338<br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Roberto Di Remigio<br></div>
</div>