Ignore/only show errors/warnings from certain directory using CMake

16,656

Solution 1

You can set compiler warning options in CMake at least for certain target or certain files.

# For target
set_target_properties(your_project_name PROPERTIES COMPILE_FLAGS "...")

# For files
set_source_files_properties(
  ${list_of_your_files}
  PROPERTIES
  COMPILE_FLAGS "..."
)

It is also possible to set the options per-folder basis by separating your project as subproject, add it using add_subdirectory(your_project) and in your project CMakeLists.txt use add_definitions(...).

From CMake documentation:

add_definitions Adds flags to the compiler command line for sources in the current directory and below.

Solution 2

Basically the same as @ronkot's answer. But don't need add_subdirectory for certain directory, using set_source_files_properties with file(GLOB_RECURSE ...) also works.

file(GLOB_RECURSE SRC_DIR "SRC_DIR/*.c" "SRC_DIR/*.h")

set_source_files_properties(
  ${SRC_DIR}
  PROPERTIES
  COMPILE_FLAGS "-w"
)

Solution 3

Expanding on @ronkot answer, the recommended target-based approach would be:

target_compile_options(your_target SCOPE compiler-warning-options)

Where SCOPE is PUBLIC|INTERFACE|PRIVATE, and compiler-warning-options are actual compiler warning options, such as -Wall. See cmake's docs for more info.

In general, prefer a target-based approach with modern CMake (3.0+) rather than a directory-based approach.

Share:
16,656
hardmooth
Author by

hardmooth

BY DAY: mathematician and coder. BY NIGHT: juggler, musician, typesetter Focus on smart algorithms, coding style and clean structures (including strict code alignment). experience in C/C++, Python, R, LaTeX, Lilypond, Perl, Bash, Qt, ...

Updated on June 05, 2022

Comments

  • hardmooth
    hardmooth about 2 years

    main question: Is there a configuration for cmake, to show or ignore compiler warnings/errors only from a certain directory?

    alternative solution: How can I toggle this in QtCreator?

    background / motivation: I'm working on a bigger CMake-project and want to focus on warnings and errors only from my subproject. I'm working with QtCreator and it annoys me to look for "my" errors/warnings under a pile of foreign ones.

  • Alexis Wilke
    Alexis Wilke about 4 years
    Interesting. The set_source_files_properties() is per directory. So all named targets are affected, whatever the project it's in...
  • Alexis Wilke
    Alexis Wilke about 4 years
    Note: This property [a.k.a. COMPILE_FLAGS] has been superseded by the COMPILE_OPTIONS property. In case you are using newer versions of cmake.