12

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?

fenix688
  • 2,435
  • 2
  • 17
  • 23
  • 1
    Tell the flatbuffers developers not to ship a find module. That's putting the map with the treasure. Ship a config file instead. http://www.cmake.org/cmake/help/v3.0/manual/cmake-packages.7.html – steveire Sep 16 '14 at 00:33

1 Answers1

16

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()
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455