0

In a CMake C++ project which uses CMAKE_CXX_FLAGS, how can I change or remove flags coming from CMAKE_CXX_FLAGS for a specific target?

I've tried the following, which doesn't remove the flags from CMAKE_CXX_FLAGS:

# Set somewhere else in the project
set(CMAKE_CXX_FLAGS "--bad-flag")

add_executable(main
    main.cpp
)

# Not actually removing the bad flag
target_compile_options(main PRIVATE "")
set_target_properties(main PROPERTIES
    COMPILE_FLAGS ""
    COMPILE_OPTIONS ""
)

This question is related, but doesn't answer my question: Does set_target_properties in CMake override CMAKE_CXX_FLAGS?

merlinND
  • 799
  • 8
  • 15
  • What about [this](https://stackoverflow.com/a/24239421/3987854) or [this](https://stackoverflow.com/a/49216539/3987854) answer? These show how to *read* the `COMPILE_OPTIONS` property for a particular target, modify the property, then **set** the updated property for the target. Note, `COMPILE_FLAGS` is deprecated and should *not* be used. – Kevin Jul 08 '20 at 12:16
  • Does this answer your question? [CMake: how to clear target compile options](https://stackoverflow.com/questions/50512905/cmake-how-to-clear-target-compile-options). This seems more directly related to your situation, in which you want to **remove** a *specific* flag for a specific target. – Kevin Jul 08 '20 at 12:22
  • Thanks for your suggestions @squareskittles, I found an exact duplicate via one of your links. In the end, the easiest solution is to add the "opposite" flag to the target without removing anything, if it exists (e.g. `--no-bad-flag`). – merlinND Jul 08 '20 at 13:33

0 Answers0