target_compile_definitions with several flags

fenix688 picture fenix688 · Sep 15, 2014 · Viewed 14.3k times · Source

I'm trying to change the flatbuffers library CMakeLists.txt to make PUBLIC some flags. Then, I've rewritten these lines:

Original code

elseif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra") 
endif()

New code

elseif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    target_compile_definitions(flatbuffers PUBLIC "-std=c++0x -Wall -pedantic -Werror -Wextra")
endif()

The target name is flatc and, when I start to build the project, I receive this:

Scanning dependencies of target flatc
[  7%] Building CXX object CMakeFiles/flatc.dir/src/idl_parser.cpp.obj
<command-line>:0:1: error: macro names must be identifiers

Is it wrong the conversion of "set(CMAKE_CXX_FLAGS )" to "target_compile_definitions()" with these flags?

Answer

The difference is that the old variable CMAKE_CXX_FLAGS is a single space-separated string with all problems (escaping, manipulation etc.) that this brings.

The newer properties (like those set by target_compile_definitions()) work on CMake lists instead, one option per list element. So just remove the quotes and you're set. And, since you're not actually passing definitions (macros), but options, use target_compile_options() instead:

elseif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    target_compile_options(flatbuffers PUBLIC -std=c++0x -Wall -pedantic -Werror -Wextra)
endif()