Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress warnings for 'void*' to 'foo*' conversions (reduced from errors by -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?

like image 231
David X Avatar asked Oct 06 '13 22:10

David X


People also ask

How do I disable warning treatment as error?

You can make all warnings being treated as such using -Wno-error. You can make specific warnings being treated as such by using -Wno-error=<warning name> where <warning name> is the name of the warning you don't want treated as an error. If you want to entirely disable all warnings, use -w (not recommended).

How do you stop warnings in Cmake?

Use -Wno-dev to suppress it. You can disable the warning like this when you are configuring your build.

How do I stop a GCC warning?

If the value of y is always 1, 2 or 3, then x is always initialized, but GCC doesn't know this. To suppress the warning, you need to provide a default case with assert(0) or similar code. This option also warns when a non-volatile automatic variable might be changed by a call to longjmp.

How do you ignore warnings in C++?

To disable a set of warnings for a given piece of code, you have to start with a “push” pre-processor instruction, then with a disabling instruction for each of the warning you want to suppress, and finish with a “pop” pre-processor instruction.


2 Answers

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.

like image 136
Crowman Avatar answered Sep 21 '22 15:09

Crowman


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

like image 35
4pie0 Avatar answered Sep 21 '22 15:09

4pie0