Check for compiler warnings in CMake projects.
This module enables compiler warnings to be checked easily in CMake projects. By default, it enables all recommended warning flags on targets across different compilers, preventing users from having to manually specify warning flags to be enabled.
This module provides two utility functions for checking compiler warnings:
target_check_warning
: For checking compiler warnings on a single target.add_check_warning
: For checking compiler warnings on all targets in the current directory.
These functions enable all recommended warning flags on the targets and can optionally be set to treat the warnings as errors.
- Supports checking warnings on MSVC, GNU, and Clang compilers.
- Optionally treats warnings as errors.
- Simple syntax and easy integration.
The recommended way to integrate this module into a project is by downloading it during the project configuration using the file(DOWNLOAD)
function:
file(
DOWNLOAD https://github.com/threeal/CheckWarning.cmake/releases/download/v3.2.0/CheckWarning.cmake
${CMAKE_BINARY_DIR}/cmake/CheckWarning.cmake
EXPECTED_MD5 8f9c3170e816ba99a3ddc6848a8456f0)
include(${CMAKE_BINARY_DIR}/cmake/CheckWarning.cmake)
Alternatively, to support offline mode, this module can also be vendored directly into a project and included normally using the include
function.
Use the target_check_warning
function to check for compiler warnings on a single target.
add_executable(main main.cpp)
target_check_warning(main)
If the TREAT_WARNINGS_AS_ERRORS
option is specified, it treats all warnings from the target as errors. This will cause the build process to fail when a warning is triggered.
target_check_warning(main TREAT_WARNINGS_AS_ERRORS)
Alternatively, the add_check_warning
function can be used to check for compiler warnings on all targets in the current directory.
add_check_warning(TREAT_WARNINGS_AS_ERRORS)
add_library(lib lib.cpp)
add_executable(main main.cpp)
To retrieve the warning flags without adding them to a target, use the get_warning_flags
function.
get_warning_flags(FLAGS)
message("Warning flags: ${FLAGS}")
Use the TREAT_WARNINGS_AS_ERRORS
option to also include the flag that treats warnings as errors.
get_warning_flags(FLAGS TREAT_WARNINGS_AS_ERRORS)
This variable contains the version of the included CheckWarning.cmake
module.
Retrieves warning flags based on the current compiler.
get_warning_flags(<output_var> [TREAT_WARNINGS_AS_ERRORS])
This function retrieves the warning flags for the current compiler and saves them to the variable <output_var>
. It determines the compiler using the CMAKE_CXX_COMPILER_ID
variable and retrieves the corresponding warning flags, as shown in the table below:
Compiler | Warning Flags |
---|---|
MSVC | /permissive- /W4 /EHsc |
GNU or Clang | -Wall -Wextra -Wpedantic |
If the TREAT_WARNINGS_AS_ERRORS
option is specified, it appends the flag that treats warnings as errors, as shown in the table below:
Compiler | Flag |
---|---|
MSVC | /WX |
GNU or Clang | -Werror |
If the compiler is simulating another compiler, determined by the existence of the CMAKE_CXX_SIMULATE_ID
variable, it will use that variable to determine the warning flags.
For compilers not listed in the table above, this function will trigger a fatal error, indicating that the compiler is unsupported.
Enables warning checks on a specific target.
target_check_warning(<target> [TREAT_WARNINGS_AS_ERRORS])
This function enables warning checks on the <target>
by appending warning flags from the get_warning_flags
function to the compile options of that target. It is equivalent to calling the target_compile_options
command on the target with the warning flags.
If the TREAT_WARNINGS_AS_ERRORS
option is specified, it will also append the flag that treats warnings as errors.
Enables warning checks on all targets in the current directory.
add_check_warning([TREAT_WARNINGS_AS_ERRORS])
This function enables warning checks on all targets in the current directory by appending warning flags from the get_warning_flags
function to the default compile options. It is equivalent to calling the add_compile_options
command with the warning flags.
If the TREAT_WARNINGS_AS_ERRORS
option is specified, it will also append the flag that treats warnings as errors.
This project is licensed under the terms of the MIT License.
Copyright © 2023-2024 Alfi Maulana