CMake and compiler warnings

I use CMake to create unix makefiles. After that, I compile the project using the make utility. The problem is that I do not see any warnings! For example, this leads to a clean build without warning:

 #include <iostream> class Foo { int first; int second; public: Foo(int a, int b) : second(a) // invalid initialization order , first(b) { } }; int main(int argc, char** argv) { int unused; // unused variable int x; double y = 3.14159; x = y; // invalid cast Foo foo(1,2); std::cout << y << std::endl; return 0; } 

Unused and lossy variables - no warnings! My CMakeLists.txt file is minimalistic:

 cmake_minimum_required(VERSION 2.8) add_executable(main main.cpp) 

When I run CMake and then make my output looks like this:

 [100%] Building CXX object CMakeFiles/main.dir/main.cpp.o Linking CXX executable main [100%] Built target main 

But when I add this line of code:

 #warning ("Custom warning") 
Result

contains a warning:

 [100%] Building CXX object CMakeFiles/main.dir/main.cpp.o ../src/main.cpp:15:2: warning: #warning ("Custom Warning") [-Wcpp] Linking CXX executable main [100%] Built target main 

I am using Ubuntu 12.04 LTS and GCC as a compiler. Perhaps CMake passes some flag to the compiler, which leads to the absence of warnings. How can I check this? I can not read make files created by CMake, they are a little cryptic.

+11
gcc cmake
Jan 09 '13 at 8:46
source share
2 answers

Positions in compiler warnings are separated. There are proponents of packages who will tell you that they know what they are doing, and compiler warnings should be ignored anyway. (I think they cannot be more erroneous.) But I think that is why CMake basically leaves the warning settings on its own.

If you want to be a little more complicated in this matter, check the use of the compiler and add a flag to a specific property of a particular target.

Apply to one goal

 if ( CMAKE_COMPILER_IS_GNUCC ) target_compile_options(main PRIVATE "-Wall") endif() if ( MSVC ) target_compile_options(main PRIVATE "/W4") endif() 

Apply to all goals.

 if ( CMAKE_COMPILER_IS_GNUCC ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") endif() if ( MSVC ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") endif() 

Note: add -Werror for GCC or / WX for MSVC to handle all warnings as errors. This applies to all warnings as errors. This can be convenient for new projects to ensure strict warning.

In addition, -Wall does not mean "all errors"; historically, it meant "all errors that everyone could agree on." Start with -Wall -Wextra , then read the manual for your version of GCC and find out what else the compiler can do for you regarding warnings.

+17
Jan 09 '13 at 12:19
source share

Solve the problem with this line of code:

 add_definitions ("-Wall") 

The 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 
-3
Jan 09 '13 at 11:30
source share



All Articles