11

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?

David X
  • 3,998
  • 3
  • 29
  • 32
  • 4
    You can fix the compiler or you can fix your code, 1989 was a long time ago. Get ahead by writing an explicit (cast) in your code. – Hans Passant Oct 06 '13 at 22:24
  • 2
    Suppressing errors won't make the errors go away. There's no point saying you want to compile C as C++ and then complaining that you don't have valid C++. If you want to compile it as C++, then convert it to valid C++, or compile it as C. – Crowman Oct 06 '13 at 22:34
  • 2
    (Okay, apparrently stackoverflow reorders tags; removed `c++` for now.) I am interested *specifically* in telling `g++` (not any random c++ implementation) to ignore these warnings and in what the equivalent of `-Wno-` is for `permissive`. – David X Oct 07 '13 at 00:46
  • 1
    I'm not sure you understand what these flags do. `-fpermissive` downgrades *some* errors into warnings, and `-Werror` upgrades all warnings to errors. `-Wno-error` causes a specified warning which `-Werror` has upgraded to an error to be downgraded again to a warning. `-Wno-error` will not work for you, here, because you have an *error* to begin with, not a warning which has been upgraded to one. If you really insist on doing this, then just use `-fpermissive` and don't use `-Werror` at all, since it's crazy to use the latter when you're deliberately trying to compile non-conforming code. – Crowman Oct 07 '13 at 02:42
  • 3
    @PaulGriffiths, actually, I do understand what these flags do. If you understand *how* they do that, and know that (for example) {they can't ignore `permissive` because it's not handled by the normal `[-Werror=]` mechanisms}, could you post the explanation of that as a answer so I can accept it? A 'no, that's not possible because this' answer, while dissapointing, would actually be a answer, unlike a 'no, that's not what you want, even though you tried to make it clear that that's *exactly* what you want' non-answer. – David X Oct 07 '13 at 03:23
  • @DavidX: I added an answer for you. – Crowman Oct 07 '13 at 03:36

2 Answers2

9

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.

Crowman
  • 25,242
  • 5
  • 48
  • 56
  • 1
    To be precise, this conversion is semantically incorrect, not syntactically, like e.g. missing braces or now-reserved identifiers. – user3125367 Jan 13 '17 at 16:47
1

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

4pie0
  • 29,204
  • 9
  • 82
  • 118
  • 3
    +1 It should probably be noted that this is what happens when you compile C code with a C++ compiler. Contrary to what is taught in academia, one is *not* an all-encompassing subset of the other. (and use `static_cast(buffer)`, btw.) – WhozCraig Oct 06 '13 at 22:33
  • @WhozCraig Indeed. I have actually had an academic not only make that claim to me but also back it up with a wholly spurious, fabricated quote from the C++ standard, which of course says precisely the opposite in reality. – user207421 Oct 06 '13 at 23:30
  • 1
    Actually if the source is C, then he would have a malloc() rather than any new. Another *little* difference between C and C++. – Alexis Wilke Oct 06 '13 at 23:35