28

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.

Evgeny Lazin
  • 9,193
  • 6
  • 47
  • 83
  • 1
    Possible duplicate of [Create custom #warning flags](https://stackoverflow.com/questions/4168245/create-custom-warning-flags) – Jim Fell May 15 '18 at 17:23

2 Answers2

44

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

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • 1
    @NormanBLancaster: Thank you for your suggested edit, but I don't really believe in `-Werror` (different compiler versions toss different warnings, and turning them into errors by default makes the build fragile). Repeating the conditional in `endif()` makes the code harder to read IMHO. And overwriting `CMAKE_CXX_FLAGS` isn't a good habit either... On the upside, I added `-Wextra`, so... ;-) – DevSolar Dec 12 '16 at 06:46
  • I believe it's `target_compile_options`, not `target_compile_option`. – John McFarlane Mar 07 '17 at 23:21
  • Is the argument to `endif` really required? – Nic Nov 30 '17 at 20:42
  • @QPaysTaxes: It was for a previous version of CMake. It is no longer, and I do not use it anymore. Edited. – DevSolar Nov 30 '17 at 21:03
  • @DevSolar Ah, okay. Good to know! (You missed the first `endif` in the second code block) – Nic Dec 01 '17 at 04:00
  • 4
    A modern way is to use [`add_compile_options`](https://cmake.org/cmake/help/latest/command/add_compile_options.html#example) – Mike T Mar 17 '19 at 11:13
  • The quotes around "-Wall -Wextra" are not correct, because this would make CMake generate a _single_ option '-Wall -Wextra' instead of two options '-Wall' '-Wextra'. Just write `target_compile_options(main PRIVATE -Wall -Wextra)` (tested with CMake 3.20.2) – UniversE Sep 26 '21 at 15:43
  • @UniversE: Does this make any practical difference? – DevSolar Oct 21 '21 at 17:55
  • 1
    @DevSolar yes, it does not work :-D Try `gcc '-Wall -Wextra' test.c` instead of `gcc -Wall -Wextra test.c` and observe gcc complaining with `unrecognized command line option ‘-Wall -Wextra’;` – UniversE Oct 25 '21 at 08:12
-3

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
Evgeny Lazin
  • 9,193
  • 6
  • 47
  • 83
  • Strange that it's not enabled by default. – Evgeny Lazin Jan 09 '13 at 11:35
  • 7
    This is a bad idea! `add_definitions` is passed in contexts that do not expect `-W` options, e.g. MinGW's windres, which will promptly break. Probably others too. – Catherine Feb 14 '16 at 14:28
  • Although this might not be ideal, this answer was helpful, and for that reason i think it deserved a thumbs up. The point of stack overflow, is to provide answers, and the more different approaches and answers tehre are to a problem, the better, as long as people are aware of the pros and cons. – Owl Jun 30 '21 at 23:14
  • In the cmake 3.0.2 documentation which is still online https://cmake.org/cmake/help/v3.0/command/add_definitions.html, add_definitions is described and there is no language like that in the 3.1.3 documentation that says that it "should" be restricted to your -D options. In the 3.23.0 documentation it specifically says that other commands should be used because there are 3 other commands that are recommended. I have a bit of code that uses cmake 2.8 so we use include_directories() and add_definitions() and that's it. The smart move is to follow this answer and its comments if your cmake is old. – cardiff space man Mar 30 '22 at 16:22