How to suppress warnings for 'void*' to 'foo*' conversions (reduced from errors by -fpermissive)

前端 未结 2 1941
执笔经年
执笔经年 2021-02-14 13:58

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 cha         


        
2条回答
  •  抹茶落季
    2021-02-14 14:32

    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.

提交回复
热议问题