禁用不兼容选项的gcc警告

时间:2022-05-26 19:14:50

I'm curious if there is an option to disable gcc warnings about a parameter not being valid for the language being compiled.

我很好奇是否有一个选项可以禁用关于参数对编译语言无效的gcc警告。

Ex:

cc1: warning: command line option "-Wno-deprecated" is valid for C++/Java/ObjC++ but not for C

Our build system passes the warnings we have decided on globally across a build. We have both C/C++ code and the warnings get real annoying when trying to find actual warnings.

我们的构建系统传递了我们在整个构建中全局决定的警告。我们有C / C ++代码,并且在尝试查找实际警告时,警告会变得非常烦人。

Any suggestions?

5 个解决方案

#1


It seems to me that if there were such an option, there would have to be a further option to turn off warnings about that option, and so on infinitely. So I suspect there isn't.

在我看来,如果有这样一个选项,就必须有另一个选项来关闭关于该选项的警告,等等无限。所以我怀疑没有。

Having the same options for builds of completely different languages seems a bit odd anyway - I would have different options defined as makefile macros and used appropriately for different targets.

对于完全不同语言的构建具有相同的选项似乎有点奇怪 - 我将不同的选项定义为makefile宏并适当地用于不同的目标。

#2


To enable specific warnings in gcc -Wxxxx and to disable them with -Wno-xxxx.

要在gcc -Wxxxx中启用特定警告,并使用-Wno-xxxx禁用它们。

From the GCC Warning Options:

从GCC警告选项:

You can request many specific warnings with options beginning -W', for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning-Wno-' to turn off warnings; for example, -Wno-implicit. This manual lists only one of the two forms, whichever is not the default.

您可以使用以-W'开头的选项请求许多特定警告,例如-Wimplicit以请求隐式声明的警告。这些特定警告选项中的每一个也都有一个负面形式 - Wno-'以关闭警告;例如,-Wno-implicit。本手册仅列出两种形式中的一种,不是默认值。

However @Neil is right about separating options for different languages. If you use make you can e.g. put all C options into CFLAGS and all C++ options into CCFLAGS.

但是@Neil对于分离不同语言的选项是正确的。如果你使用make,你可以例如将所有C选项放入CFLAGS,将所有C ++选项放入CCFLAGS。

#3


If you set the CFLAGS variable it will only effect C files, if you set CXXFLAGS variable it will only effect C++ files, so you can easily separate the logic.

如果设置CFLAGS变量,它只会影响C文件,如果设置CXXFLAGS变量,它只会影响C ++文件,因此您可以轻松地分离逻辑。

#4


Alas gcc doesn't have ability to turn individual warnings on or off. Some warnings are special cased, and warning levels can be set, but no way to turn off warnings you don't care about.

唉gcc无法打开或关闭个别警告。某些警告是特殊的,可以设置警告级别,但无法关闭您不关心的警告。

I mean who would ever care about missing empty line at the end of the file? I know there is a patch to turn it off, but building my complier from source is a bit too much.

我的意思是谁会关心在文件末尾丢失空行?我知道有一个补丁可以关闭它,但是从源代码构建我的编译器有点太多了。

For your build system you might want to define separate sets of warnings for different languages. What will happen if you need to use some other compiler, not gcc?

对于构建系统,您可能希望为不同的语言定义单独的警告集。如果你需要使用其他编译器,而不是gcc会发生什么?

#5


If compiling your C files as C++ is an option for you, -x c++.

如果将C文件编译为C ++是一个选项,-x c ++。

#1


It seems to me that if there were such an option, there would have to be a further option to turn off warnings about that option, and so on infinitely. So I suspect there isn't.

在我看来,如果有这样一个选项,就必须有另一个选项来关闭关于该选项的警告,等等无限。所以我怀疑没有。

Having the same options for builds of completely different languages seems a bit odd anyway - I would have different options defined as makefile macros and used appropriately for different targets.

对于完全不同语言的构建具有相同的选项似乎有点奇怪 - 我将不同的选项定义为makefile宏并适当地用于不同的目标。

#2


To enable specific warnings in gcc -Wxxxx and to disable them with -Wno-xxxx.

要在gcc -Wxxxx中启用特定警告,并使用-Wno-xxxx禁用它们。

From the GCC Warning Options:

从GCC警告选项:

You can request many specific warnings with options beginning -W', for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning-Wno-' to turn off warnings; for example, -Wno-implicit. This manual lists only one of the two forms, whichever is not the default.

您可以使用以-W'开头的选项请求许多特定警告,例如-Wimplicit以请求隐式声明的警告。这些特定警告选项中的每一个也都有一个负面形式 - Wno-'以关闭警告;例如,-Wno-implicit。本手册仅列出两种形式中的一种,不是默认值。

However @Neil is right about separating options for different languages. If you use make you can e.g. put all C options into CFLAGS and all C++ options into CCFLAGS.

但是@Neil对于分离不同语言的选项是正确的。如果你使用make,你可以例如将所有C选项放入CFLAGS,将所有C ++选项放入CCFLAGS。

#3


If you set the CFLAGS variable it will only effect C files, if you set CXXFLAGS variable it will only effect C++ files, so you can easily separate the logic.

如果设置CFLAGS变量,它只会影响C文件,如果设置CXXFLAGS变量,它只会影响C ++文件,因此您可以轻松地分离逻辑。

#4


Alas gcc doesn't have ability to turn individual warnings on or off. Some warnings are special cased, and warning levels can be set, but no way to turn off warnings you don't care about.

唉gcc无法打开或关闭个别警告。某些警告是特殊的,可以设置警告级别,但无法关闭您不关心的警告。

I mean who would ever care about missing empty line at the end of the file? I know there is a patch to turn it off, but building my complier from source is a bit too much.

我的意思是谁会关心在文件末尾丢失空行?我知道有一个补丁可以关闭它,但是从源代码构建我的编译器有点太多了。

For your build system you might want to define separate sets of warnings for different languages. What will happen if you need to use some other compiler, not gcc?

对于构建系统,您可能希望为不同的语言定义单独的警告集。如果你需要使用其他编译器,而不是gcc会发生什么?

#5


If compiling your C files as C++ is an option for you, -x c++.

如果将C文件编译为C ++是一个选项,-x c ++。