Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gcc CMake和编译器警告_Gcc_Cmake - Fatal编程技术网

Gcc CMake和编译器警告

Gcc CMake和编译器警告,gcc,cmake,Gcc,Cmake,我使用CMake生成unix生成文件。之后,我使用make实用程序编译项目。 问题是我看不到任何警告!例如,这会导致无警告的干净生成: #include <iostream> class Foo { int first; int second; public: Foo(int a, int b) : second(a) // invalid initialization order , first(b) { } }; int

我使用
CMake
生成unix生成文件。之后,我使用
make
实用程序编译项目。 问题是我看不到任何警告!例如,这会导致无警告的干净生成:

#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;
}
当我运行
cmake
然后运行
make
时,我的输出如下所示:

[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
Linking CXX executable main
[100%] Built target main
[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
但当我添加这行代码时:

#warning ("Custom warning")
add_definitions ("-Wall")
结果输出包含警告:

[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

我使用Ubuntu12.04LTS和GCC作为编译器。也许CMake会将一些标志传递给编译器,从而导致没有警告。我怎么检查?我无法读取CMake生成的Make文件,它们有点神秘。

用这行代码解决问题:

#warning ("Custom warning")
add_definitions ("-Wall")
现在的结果如下所示:

[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
Linking CXX executable main
[100%] Built target main
[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

编译器警告的位置是分开的。有些包维护人员会告诉您,他们知道自己在做什么,在任何情况下都应该忽略编译器警告。(我认为他们的错误再大不过了。)但我想这就是为什么CMake大多不使用警告设置的原因

如果您想更复杂一点,请检查正在使用的编译器,并将该标志添加到特定目标的特定属性中

适用于单个目标 适用于所有目标 注:为GCC添加
-Werror
,或为MSVC添加
/WX
,以将所有警告视为错误。这会将所有警告视为错误。这对于新项目实施严格警告非常方便


另外,
-Wall-Wextra
并不意味着“所有错误”;历史上,
-Wall
意味着“所有人都能同意的错误”,而
-Wextra
则意味着“更多”。从这一点开始,然后仔细阅读您的GCC版本手册,找出编译器在警告方面还能为您做些什么…

奇怪的是,默认情况下它没有启用。这是个坏主意
add_definitions
在不需要
-W
选项的上下文中传递,例如MinGW的windres,它将立即中断。可能还有其他人。@NormanBLancaster:谢谢你建议的编辑,但我并不真的相信
-Werror
(不同的编译器版本会抛出不同的警告,默认情况下将它们变成错误会使构建变得脆弱)。重复
endif()
中的条件会使代码更难阅读。而且覆盖CMAKE\U CXX\U标志也不是一个好习惯。。。在上面,我添加了
-Wextra
,所以…;-)我相信这是
target\u compile\u options
,而不是
target\u compile\u option
endif
的参数真的需要吗?@QPaysTaxes:这是针对CMake的早期版本。它不再是了,我也不再使用它了。编辑。现代的方法是使用可能的副本