[CMake] Handling of generator expressions

Roberto Di Remigio roberto.diremigio at gmail.com
Sun Nov 19 13:16:10 EST 2017


Hello,
I am trying to compile the following code snippet:

int main(int argc, char ** argv) {
  int stack_array[100];
  stack_array[1] = 0;
  return stack_array[argc + 100]; // BOOM
}

The followin CMakeLists.txt works fine, but you can notice that
target_compile_options takes a list, while target_link_libraries takes a
string. Always using the string will result in a compilation error, because
the compiler flags are passed quoted to the compiler. Always using the list
doesn't work either, because the generator expression is evaluated as
$<1:-fsanitize=address -fno-omit-frame-pointer> and then passed as-is to
the linker, which doesn't know what to do with it.
Is this supposed to be like this? If yes, what's the rationale?
Thanks in advance!
  Roberto

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(recipe-09 CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

list(APPEND CXX_BASIC_FLAGS "-g3" "-O1")

include(CheckCXXCompilerFlag)

set(ASAN_FLAGS "-fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_REQUIRED_FLAGS ${ASAN_FLAGS})
check_cxx_compiler_flag(${ASAN_FLAGS} asan_works)
unset(CMAKE_REQUIRED_FLAGS)

add_executable(asan-example asan-example.cpp)
string(REPLACE " " ";" _asan_flags ${ASAN_FLAGS})
target_compile_options(asan-example
  PUBLIC
    ${CXX_BASIC_FLAGS}
    $<$<BOOL:${asan_works}>:${_asan_flags}>
  )
target_link_libraries(asan-example
  PUBLIC
    $<$<BOOL:${asan_works}>:${ASAN_FLAGS}>
  )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20171119/83815f5b/attachment.html>


More information about the CMake mailing list