在Visual C++中,可以使用#pragma warning (disable: ...)
.我还发现在GCC中你可以覆盖每个文件的编译器标志.我怎样才能为"下一行"做这个,或者使用GCC围绕代码区域推送/弹出语义?
我正在寻找一种方法来抑制我可能通过pragma指令获得的所有可能的警告.我制作了一些警卫宏来帮助我让第三方标题从警告中消失,而现在它们对于msvc和clang来说就像魅力一样.我仍然缺少使用Gcc诊断编译指示的正确方法来抑制部分中的每个警告.我举几个例子:
在msvc中我们可以这样做:
#pragma warning(push, 0)
// Code that produces warnings...
#pragma warning(pop)
Run Code Online (Sandbox Code Playgroud)
在clang我们可以做到这一点:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wall"
#pragma clang diagnostic ignored "-Wextra"
// Code that produces warnings...
#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)
中间的代码现在正在被警告保持沉默.
在Gcc中我们也有类似的pragma指令与clang,我想我可以尝试这样的事情:
#pragma GCC diagnostic push
#pramga GCC diagnostic ignored "-Wall"
#pragma GCC diagnostic ignored "-Wextra"
// Code that produces warnings...
#pramga GCC diagnostic pop
Run Code Online (Sandbox Code Playgroud)
但是在GCC中传递-Wall和-Wextra在诊断忽略的pragma中并不像clang那样工作,并且不会禁用所有可能的警告.而不是传递一个特定的警告来禁用工作:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
void foo (int x) // No longer getting "unused …
Run Code Online (Sandbox Code Playgroud) 有没有办法用automake在每个文件的基础上设置标志?
特别是,如果我有一个c ++项目并想用-WAll编译所有文件,除了我要禁用特定警告的文件,我该怎么办?
我尝试过类似的东西:
CXXFLAGS = -WAll ...
bin_PROGRAMS = test
test_SOURCES = main.cpp utility.cpp
utility_o_CXXFLAGS = $(CXXFLAGS) -Wno-unused-value
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
编辑:删除了对automake手册的引用,这实际上是误导(感谢Douglas Leeder).
我有几个文件,我想在其中严格警告警告,并且我使用 GCC 来构建我的项目。
我已经#pragma GCC diagnostic error "-Wall"
按照https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Diagnostic-Pragmas.html尝试过,但它没有考虑到其他一些启用的警告类型:
foo.c:666:6: warning: passing argument 2 of 'bar' from incompatible pointer type [-Wincompatible-pointer-types]
Run Code Online (Sandbox Code Playgroud)
有没有办法为文件启用 -Werror 就像它是从命令行提供的一样(或者,至少,对于隐式启用的警告集),所以任何警告都会触发错误?