Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake and compiler warnings

Tags:

gcc

cmake

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, this results in clean build without warnings:

#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 variable and lossy variable cast - 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")

resulting output contains 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 use Ubuntu 12.04 LTS and GCC as a compiler. Maybe CMake passes some flag to compiler that results in absence of warnings. How can I check it? I can't read makefiles generated by CMake, they are a little bit cryptic.

like image 359
Evgeny Lazin Avatar asked Jan 09 '13 08:01

Evgeny Lazin


People also ask

How do you stop warnings in CMake?

cmake:48 (include) CMakeLists. txt:208 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. You can disable the warning like this when you are configuring your build.

Does CMake contain a compiler?

About CMake. CMake is an extensible, open-source system that manages the build process in an operating system and in a compiler-independent manner.

How do I turn off GCC warnings?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

How do I ignore a warning in Makefile?

Maybe you can look for CFLAGS options in Makefile and remove the -Werror flag. The Werror flag will make all warnings into errors.


1 Answers

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.

Apply to a Single Target

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

Apply to All Targets

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...

like image 156
DevSolar Avatar answered Oct 11 '22 04:10

DevSolar