CMakeToolsCompileOptions¶
This module provides a strict set of C++ compile options. To use the options, include this file:
include(CMakeToolsCompileOptions)
Result Variables¶
This module defines these variables:
- CMAKE_TOOLS_COMPILE_OPTIONS¶
A list of C++ compile options that set strict warnings and errors. To use the options, pass them to target_compile_options():
add_executable(app main.cpp) target_compile_options(app PRIVATE ${CMAKE_TOOLS_COMPILE_OPTIONS})
- CMAKE_TOOLS_LINK_LIBRARIES¶
A list of libraries to pass to the linker. To use the libraries, pass them to target_link_libraries():
add_executable(app main.cpp) target_link_libraries(app PRIVATE ${CMAKE_TOOLS_LINK_LIBRARIES})
Warning
By default, you should place these first in the list of link dependencies. Some libraries, such as
asan
, must be first on the link command.add_executable(app main.cpp) target_link_libraries(app PRIVATE ${CMAKE_TOOLS_LINK_LIBRARIES} Qt5::Core)
To see the exact options for your compiler, print the variable:
include(CMakePrintHelpers)
cmake_print_variables(CMAKE_TOOLS_COMPILE_OPTIONS CMAKE_TOOLS_LINK_LIBRARIES)
If there are specific options that you don’t want to use, just remove them from the list:
list(REMOVE_ITEM CMAKE_TOOLS_COMPILE_OPTIONS -Werror)
Hints¶
You can control some of the options with these variables. Define them before including this module.
- CMAKE_TOOLS_ADDRESS_SANITIZER¶
Add compile options to
CMAKE_TOOLS_COMPILE_OPTIONS
and asan toCMAKE_TOOLS_LINK_LIBRARIES
to build with address sanitizer. The options and library only apply to debug builds.Warning
You cannot set
CMAKE_TOOLS_THREAD_SANITIZER
andCMAKE_TOOLS_ADDRESS_SANITIZER
together. The module will print an error, then continue the configuration step.
- CMAKE_TOOLS_COVERAGE¶
Add compile options to
CMAKE_TOOLS_COMPILE_OPTIONS
and gcov toCMAKE_TOOLS_LINK_LIBRARIES
to enable test coverage.
- CMAKE_TOOLS_THREAD_SANITIZER¶
Add compile options to
CMAKE_TOOLS_COMPILE_OPTIONS
and tsan toCMAKE_TOOLS_LINK_LIBRARIES
to build with thread sanitizer. The options and library only apply to debug builds.Warning
You cannot set
CMAKE_TOOLS_THREAD_SANITIZER
andCMAKE_TOOLS_ADDRESS_SANITIZER
together. The module will print an error, then continue the configuration step.
- CMAKE_TOOLS_UB_SANITIZER¶
Add compile options to
CMAKE_TOOLS_COMPILE_OPTIONS
and ubsan toCMAKE_TOOLS_LINK_LIBRARIES
to build with undefined behavior sanitizer. The options and library only apply to debug builds.
Examples¶
Here are examples for some use cases.
# Get the default compile options, but don't treat warnings as errors.
include(CMakeToolsCompileOptions)
list(REMOVE_ITEM CMAKE_TOOLS_COMPILE_OPTIONS -Werror)
add_executable(app app.cpp)
target_compile_options(app PRIVATE ${CMAKE_TOOLS_COMPILE_OPTIONS})
# Add address and undefined behavior sanitizers to a unit test.
set(CMAKE_TOOLS_ADDRESS_SANITIZER true)
set(CMAKE_TOOLS_UB_SANITIZER true)
include(CMakeToolsCompileOptions)
add_executable(test test.cpp)
target_compile_options(test PRIVATE ${CMAKE_TOOLS_COMPILE_OPTIONS})
target_link_libraries(test PRIVATE ${CMAKE_TOOLS_LINK_LIBRARIES} Catch2::Catch2)
# Set up to gather unit test coverage data.
set(CMAKE_TOOLS_COVERAGE true)
include(CMakeToolsCompileOptions)
add_executable(test test.cpp)
target_compile_options(test PRIVATE ${CMAKE_TOOLS_COMPILE_OPTIONS})
target_link_libraries(test PRIVATE ${CMAKE_TOOLS_LINK_LIBRARIES} Catch2::Catch2)