How to disable '-pedantic' compiler option for a specific library?

For my gcc C++ project, my top-level CMakeLists.txt contains:

add_compile_options(-Wall -pedantic)

and it builds multiple libraries using add_subdirectory() calls.

How can I disable the ‘-pedantic’ flag for one of those libraries by modifying the CMakeLists.txt file of that library?

I tried this:

  string(REPLACE " -pedantic" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  add_subdirectory(Prototypes/dpdk_test_static_library/dpdk)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic")

But it didn’t work: -pedantic still appeared in the g++ command line for the library source files.

The variable CMAKE_CXX_FLAGS does not contain the values stored by the property set by add_compile_options. You need to modify the property for the targets created downstream in Prototypes/dpdk_test_static_library/dpdk/CMakeLists.txt. After the target is created then you can modify COMPILE_OPTIONS property for that target. Or you can try to modify the directory property COMPILE_OPTIONS before calling add_subdirectory().

Thanks. How would I delete ‘-pedantic’ from COMPILE_OPTIONS in dpdk/CMakeLists.txt?

You’ll want to do something like this:

    # Remove entry
    get_target_property(target_options my_target COMPILE_OPTIONS)
    list(REMOVE_ITEM target_options "-pedantic")
    set_property(TARGET my_target PROPERTY COMPILE_OPTIONS ${target_options})

Thanks for your help.