While I can set different warning levels depending on the compiler, e.g.:
if(MSVC)
target_compile_options(${TARGET_NAME} PRIVATE /W4 /WX)
else()
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
endif()
I cannot set them on a file by file basis.
In the same directory, I have a set of files whose names are in the ${SRC_WARN}
CMake variable, which need a different warning level compared to the others.
Is there a way to specify such a condition with target_compile_options
?
You can set compile options (COMPILE_OPTIONS
) for a single file (or group of files) using set_source_files_properties()
. You can change the COMPILE_OPTIONS
for your ${SRC_WARN}
source files by adding to your existing CMake code:
if(MSVC)
target_compile_options(${TARGET_NAME} PRIVATE /W4 /WX)
# Change these files to have warning level 2.
set_source_files_properties(${SRC_WARN} PROPERTIES COMPILE_OPTIONS /W2)
else()
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
# Change these files to inhibit all warnings.
set_source_files_properties(${SRC_WARN} PROPERTIES COMPILE_OPTIONS -w)
endif()