如何将'void *'的警告抑制为'foo *'转换(由于错误而导致错误减少) [英] How to suppress warnings for 'void*' to 'foo*' conversions (reduced from errors by -fpermissive)

查看:1092
本文介绍了如何将'void *'的警告抑制为'foo *'转换(由于错误而导致错误减少)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用g ++编译一些c代码(是的,故意的)。 (例如):

 错误:从'void *'无效转换为'unsigned char *' [-fpermissive] 

加入 -fpermissive 至编译选项得到我:

 错误:从'void *'无效转换为'unsigned char *'[-Werror = permissive ] 

这似乎是错误,因为 - 错误,但是加入 -Wno-error = permissive -Wno-permissive 结果为:

 错误:-Werror = permissive:无选项-Wpermissive 
错误:无法识别的命令行选项-Wno-permissive[-Werror]

如何禁用从void *转换为其他指针类型的警告(globaly)?

解决方案

您不能禁用从 void * 到其他指针类型的转换警告,因为这不是一个警告 - 它是一个synta x error。



这里发生的事情是,您正在使用 -fpermissive 来降低一些错误 - 包括这一项 - 到警告,因此允许你编译一些不符合的代码(显然,很多类型的语法错误,比如缺少大括号,不能降级为警告,因为编译器无法知道如何修复它们他们成为可以理解的代码)。 然后,您还使用 -Werror 将所有警告升级为错误,其中包括警告 code> -fpermissive 已将您的错误转化为。

-Wno-error 仅用于否定 -Werror -Werror 将所有警告视为错误,除非<$ em $ c> -Wno-error ,它们仍然是警告。正如 -W 标志所表明的那样,这两个标志都适用于警告,所以他们对这个特定问题无能为力,因为你拥有这是一个错误。对于这种无效转换,没有警告,您可以使用 -Werror 关闭和打开,因为它不是真正的警告 - 这是一个错误,<$ c $



您可以编译您的不符合条件代码,在这种情况下,通过使用 -fpermissive 不是使用 -Werror 。这仍然会给你一个警告,但是像所有警告一​​样,它不会阻止编译成功。如果您故意尝试编译不符合的代码,那么使用 -Werror 是没有意义的,因为您知道代码包含错误,因此会导致警告,即使使用 -fpermissive ,所以指定 -Werror 等价于说请不要编译我的不合格代码,即使我



让g ++去抑制警告的最大办法是使用 -fsyntax-only ,它将检查语法错误,而不是其他任何东西,但是因为你在这里有一个语法错误,这不会对你有所帮助,而且你能做的最好的事情就是把它变成 -fpermissive


I'm trying to compile some c code with g++ (yes, on purpose). I'm getting errors like (for example):

error: invalid conversion from 'void*' to 'unsigned char*' [-fpermissive]

adding -fpermissive to the compilation options gets me:

error: invalid conversion from 'void*' to 'unsigned char*' [-Werror=permissive]

which seems to be a error because of -Werror, however adding -Wno-error=permissive -Wno-permissive results in:

error: -Werror=permissive: no option -Wpermissive
error: unrecognized command line option "-Wno-permissive" [-Werror]

How do I disable warnings (globaly) for conversions from void* to other pointer types?

解决方案

You cannot "disable warnings for conversions from void* to other pointer types" because this is not a warning - it is a syntax error.

What's happening here is that you are using -fpermissive which downgrades some errors - including this one - to warnings, and therefore allows you to compile some non-conforming code (obviously many types of syntax errors, such as missing braces, cannot be downgraded to warnings since the compiler cannot know how to fix them to turn them into understandable code).

Then, you are also using -Werror which upgrades all warnings to errors, including the "warnings" that -fpermissive has turned your error into.

-Wno-error is used only to negate -Werror, i.e. it causes -Werror to treat all warnings as errors except the warnings specified with -Wno-error, which remain as warnings. As the -W flag suggests, both these flags work with warnings, so they can't do anything with this particular issue, since what you have here is an error. There is no "warning" for this kind of invalid conversion that you can switch off and on with -Werror because it's not a real warning - it's an error that -fpermissive is merely causing to be treated as a warning.

You can compile your non-comforming code, in this case, by using -fpermissive and not using -Werror. This will still give you a warning, but like all warnings, it won't prevent successful compilation. If you are deliberately trying to compile non-conforming code, then using -Werror makes no sense, because you know your code contains errors and will therefore result in warnings, even with -fpermissive, so specifying -Werror is equivalent to saying "please do not compile my non-conforming code, even though I've just asked you to."

The furthest you can go to get g++ to suppress warnings is to use -fsyntax-only which will check for syntax errors and nothing else, but since what you have here is a syntax error, that won't help you, and the best you can do is have that turned into a warning by -fpermissive.

这篇关于如何将'void *'的警告抑制为'foo *'转换(由于错误而导致错误减少)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆