I use CMake
to generate unix makefiles. After that I compile project using make
utility.
Problem is that I can\'t see any warnings! For example, th
The positions on compiler warnings are divided. There are package maintainers who will tell you that they know what they are doing, and compiler warnings should be ignored in any case. (I think they couldn't be more wrong.) But I guess that is why CMake mostly leaves the warning settings alone.
If you want to be a bit more sophisticated about it, check for the compiler being used, and add the flag to the specific property of the specific target.
if ( CMAKE_COMPILER_IS_GNUCC )
target_compile_options(main PRIVATE "-Wall -Wextra")
endif()
if ( MSVC )
target_compile_options(main PRIVATE "/W4")
endif()
if ( CMAKE_COMPILER_IS_GNUCC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
endif()
if ( MSVC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
Note: add -Werror
for GCC or /WX
for MSVC to treat all warnings as errors. This will treat all warnings as errors. This can be handy for new projects to enforce warning strictness.
Also, -Wall -Wextra
does not mean "all errors"; historically -Wall
meant "all errors that everybody could agree on", and -Wextra
"some more". Start with that, then peruse the manual for your version of GCC, and find what else the compiler can do for you with regards to warnings...
Solve problem with this line of code:
add_definitions ("-Wall")
Result now looks like this:
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
../src/main.cpp:15:2: warning: #warning ("Custom warning") [-Wcpp]
../src/main.cpp: In constructor ‘WarnSrc::WarnSrc(int, int)’:
../src/main.cpp:6:9: warning: ‘WarnSrc::second’ will be initialized after [-Wreorder]
../src/main.cpp:5:9: warning: ‘int WarnSrc::first’ [-Wreorder]
../src/main.cpp:8:5: warning: when initialized here [-Wreorder]
../src/main.cpp: In function ‘int main(int, char**)’:
../src/main.cpp:19:9: warning: unused variable ‘unused’ [-Wunused-variable]
../src/main.cpp:20:9: warning: variable ‘x’ set but not used [-Wunused-but-set-variable]
Linking CXX executable main
[100%] Built target main