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

StackOverflow https://stackoverflow.com/questions/19214645

  •  30-06-2022
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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

Well it might require direct interaction with compiler's code. This is probably not what you want. You want to fix your code. First, these are not warnings but errors. You need to cast void* as you can not convert a void* to anything without casting it first (it is possible in C however which is less type safe as C++ is).

For example you would need to do

void* buffer = operator new(100);
unsigned char* etherhead = static_cast<unsigned char*>(buffer);
                                  ^
                                 cast

If you want a dynamically allocated buffer of 100 unsigned char then you are better off doing this

unsigned char* p = new unsigned char[100];

and avoiding the cast.

void pointers

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top