From 0f2519944ae93d788e9a9890000cb583297f33cc Mon Sep 17 00:00:00 2001 From: Esteve Fernandez Date: Fri, 16 Jun 2017 22:23:59 +0200 Subject: [PATCH] Explicitly set C11 and C++14 via the CMake variables (#141) * Explicitly set C99 and C++14 via the CMake variables * Enable passing CMAKE_C_STANDARD and CMAKE_CXX_STANDARD externally * Use add_compile_options * Be more specific about the compiler, independent from the OS * Check compiler, not OS. Use add_compile_options instead of CMAKE_C_FLAGS and CMAKE_CXX_FLAGS * add_compile_options() takes a list, not a string * switch to c11 because of redefinition warnings avoid typedef redifinition warning on macos remove todo, compiler come from redefinition of typedef --- rcl/CMakeLists.txt | 15 ++++++++++++--- rcl_lifecycle/CMakeLists.txt | 20 ++++++++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/rcl/CMakeLists.txt b/rcl/CMakeLists.txt index 26a16df..634bf82 100644 --- a/rcl/CMakeLists.txt +++ b/rcl/CMakeLists.txt @@ -12,9 +12,18 @@ find_package(rosidl_generator_c REQUIRED) include_directories(include) -if(NOT WIN32) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra") +# Default to C11 +if(NOT CMAKE_C_STANDARD) + set(CMAKE_C_STANDARD 11) +endif() + +# Default to C++14 +if(NOT CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 14) +endif() + +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + add_compile_options(-Wall -Wextra) endif() set(${PROJECT_NAME}_sources diff --git a/rcl_lifecycle/CMakeLists.txt b/rcl_lifecycle/CMakeLists.txt index 579b117..15a1eb2 100644 --- a/rcl_lifecycle/CMakeLists.txt +++ b/rcl_lifecycle/CMakeLists.txt @@ -11,6 +11,20 @@ find_package(rmw REQUIRED) include_directories(include) +# Default to C11 +if(NOT CMAKE_C_STANDARD) + set(CMAKE_C_STANDARD 11) +endif() + +# Default to C++14 +if(NOT CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 14) +endif() + +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + add_compile_options(-Wall -Wextra -Wpedantic) +endif() + set(rcl_lifecycle_sources src/com_interface.c src/default_state_machine.c @@ -43,12 +57,6 @@ install(TARGETS rcl_lifecycle RUNTIME DESTINATION bin) if(BUILD_TESTING) - if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") - add_compile_options(-Wall -Wextra -Wpedantic) - endif() - find_package(ament_cmake_gtest REQUIRED) find_package(ament_lint_auto REQUIRED) find_package(rcl REQUIRED)