0

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?

Kevin
  • 16,549
  • 8
  • 60
  • 74
Pietro
  • 12,086
  • 26
  • 100
  • 193

1 Answers1

1

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()
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • Should/can I use the PRIVATE token to specify that this is a local property? – Pietro Oct 28 '19 at 13:43
  • Is it `COMPILE_OPTIONS` or `COMPILE_FLAGS`? – Pietro Oct 28 '19 at 13:45
  • 1
    You can't use the visibility qualifiers like `PUBLIC` or `PRIVATE` with the `set_source_file_properties()` command. These properties will only be applied to those source files, and will only be visible to targets added in the same CMakeLists.txt file. And based on the documentation, you should use the newer `COMPILE_OPTIONS`; the `COMPILE_FLAGS` property is the old property. – Kevin Oct 28 '19 at 13:47