- 1 year ago
Cmake, how to remove compiler flag ONLY from executable creation
You can remove a compiler flag only from the executable creation by specifying it in the target_compile_options
command with the PUBLIC
and PRIVATE
keyword arguments.
The PUBLIC
argument specifies that the flag should be used by the target and its dependencies, while the PRIVATE
argument specifies that the flag should only be used by the target.
Here is an example CMake code snippet that removes a specific flag (-Wno-implicit-fallthrough
) only from the executable creation:
scssadd_executable(my_executable my_source_file.cpp)
target_compile_options(my_executable PRIVATE -Wno-implicit-fallthrough)
target_compile_options(my_executable PUBLIC ${MY_OTHER_COMPILE_OPTIONS})
In the above code snippet, the target_compile_options
command is used to add a compile option -Wno-implicit-fallthrough
only to the my_executable
target. Any other compile options specified in ${MY_OTHER_COMPILE_OPTIONS}
will be applied to the my_executable
target and its dependencies.
By using the PRIVATE
argument for the -Wno-implicit-fallthrough
flag, it will be removed from any dependencies that are linked against my_executable
.