How to set the warning level for a project (not the whole solution) using CMake? Should work on Visual Studio and GCC.
I found various options but most seem either not to work or are not consistent with the documentation.
Join Stack Overflow to learn, share knowledge, and build your career.
How to set the warning level for a project (not the whole solution) using CMake? Should work on Visual Studio and GCC.
I found various options but most seem either not to work or are not consistent with the documentation.
In modern CMake, the following works well:
if(MSVC)
target_compile_options(${TARGET_NAME} PRIVATE /W4 /WX)
else()
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
endif()
My colleague suggested an alternative version:
target_compile_options(${TARGET_NAME} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -pedantic -Werror>
)
Replace ${TARGET_NAME}
with the actual target name. -Werror
is optional, it turns all warnings into errors.
Or use add_compile_options(...)
if you want to apply it to all targets as suggested by @aldo in the comments.
Also, be sure to understand the difference between PRIVATE
and PUBLIC
(public options will be inherited by targets that depend on the given target).
else()
or endif()
.
– Timmmm
Feb 10 '19 at 10:58
add_compile_options()
is that the warnings will propagate to targets added via add_subdirectory()
. If you include external libraries this way you may get lots of warnings if that library was designed with different warning level.
– trozen
Oct 25 '19 at 13:10
UPDATE: This answer predates the Modern CMake era. Every sane CMake user should refrain from fiddling with CMAKE_CXX_FLAGS
directly and call the target_compile_options
command instead. Check the mrts' answer which presents the recommended best practice.
You can do something similar to this:
if(MSVC)
# Force to always compile with W4
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
# Update if necessary
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic")
endif()
/Wall
flag (which is named EnableAllWarnings
). It produces even more warnings than /W4
. However from my experience it produces way too much warnings.
– Adam Badura
May 23 '16 at 23:30
/Wall
is usable if you want to follow a 'subtractive' strategy for warnings, just like clang's -Weverything
. Instead of selecting warnings to enable, you enable everything and then select specific warnings to disable.
– bames53
Sep 7 '16 at 16:59
Some CMake modules I've written include experimental cross-platfrom warning suppression:
sugar_generate_warning_flags(
target_compile_options
target_properties
ENABLE conversion
TREAT_AS_ERRORS ALL
)
set_target_properties(
foo
PROPERTIES
${target_properties}
COMPILE_OPTIONS
"${target_compile_options}"
)
Result for Xcode:
CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION
Xcode attribute
(aka build settings -> warnings -> suspicious implicit conversions -> YES)-Werror
Makefile gcc and clang:
-Wconversion
, -Werror
Visual studio:
/WX
, /w14244
Here is the best solution I found so far (including a compiler check):
if(CMAKE_BUILD_TOOL MATCHES "(msdev|devenv|nmake)")
add_definitions(/W2)
endif()
This will set warning level 2 in Visual Studio. I suppose with a -W2
it would work in GCC too (untested).
Update from @Williams: It should be -Wall
for GCC.
-Wall
and maybe -Wextra
as detailed at gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
– Milliams
Mar 4 '10 at 14:19
-W -Wall -Wextra -pedantic
. -Wextra
IIRC replaced -W
in a later version of GCC, but I leave both for compatibilities sake.
– Jimmio92
Sep 26 '16 at 22:21
As per Cmake 3.19.2 documentation:
if (MSVC)
# warning level 4 and all warnings as errors
add_compile_options(/W4 /WX)
else()
# lots of warnings and all warnings as errors
add_compile_options(-Wall -Wextra -pedantic -Werror)
endif()
GCC and Clang share these flags, so this should cover all 3.
add_compile_options
is directory-wide, whereas target_compile_options
is just for a single target.
– TehWan
Jun 10 '20 at 1:00
if(MSVC)
string(REGEX REPLACE "/W[1-3]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()
If you use target_compile_options
- cmake will try to use double /W*
flag, which will give warning by compiler.
add_compile_options
only to get tons of warnings that /W3
is being overriden with /W4
. The fact tha CMake is not addressing this rudimentary option (setting warning level) is beyond belief.
– Resurrection
Apr 25 '20 at 18:16