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

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.

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

Related

g++ compilation fails when profiling enabled

I have a working program, which I want to profile. Thus, I just added a -pg switch to the Makefile. This gives thousands of repetitions of this same message:
/tmp/ccOlI4CC.s:62095: Error: bad expression
/tmp/ccOlI4CC.s:62095: Error: junk `mcount#GOTPCREL(%rip)' after expression
/tmp/ccOlI4CC.s:62417: Error: bad expression
/tmp/ccOlI4CC.s:62417: Error: junk `mcount#GOTPCREL(%rip)' after expression
I'm in the blue. What can I do about this?
The problem was solved by removing the -masm=intel flag.

Error while compiling wxWidgets with TDM-GCC

I've been trying to compile wxWidgets for a while now, I've been using TDM-GCC and have been following the guide here with no such luck. The last few lines of my mingw32-make -f makefile.gcc SHARED=1 UNICODE=1 BUILD=release MONOLITHIC=1 is
../../src/msw/thread.cpp: In member function 'void wxThread::Exit(wxThread::Exit
Code)':
../../src/msw/thread.cpp:1165:28: error: cast from 'wxThread::ExitCode {aka void
*}' to 'unsigned int' loses precision [-fpermissive]
_endthreadex((unsigned)status);
makefile.gcc:4957: recipe for target 'gcc_mswudll\monodll_thread.o' failed
mingw32-make: *** [gcc_mswudll\monodll_thread.o] Error 1
Anyone got any ideas? Please help, I'm truly baffled.
With the help of #ravenspoint, and some googling, I found this, which mentioned passing the variable CXXFLAGS to pass arguments to g++ (and for gcc, use CFLAGS), then from #ravenspoint's answer I added CXXFLAGS+=-fpermissive to my mingw32-make -f makefile.gcc SHARED=1 UNICODE=1 BUILD=release MONOLITHIC=1, to pass -fpermissive to g++.
{aka void*} to unsigned int loses precision [-fpermissive]
That occurs when it expects 32 bit and gets a 64 bit, usually a problem in compiling code meant to be 32 bit in the TDM default -m64 bit mode.
For void* to lose precision, it must be a 64 bit variable, and unsigned int must be 32.
If you downgrade the variable by disabling the warning and run it on 64 bit machine, you could get a pointer error crash while running program.

Parse issue Expected identifier or ')' in _types.h

Suddenly I'm getting an error in _types.h on the line with typedef. How can I track this down?
#ifdef __GNUC__
typedef __signed char __int8_t;
#else /* !__GNUC__ */
This is pretty much always caused by a syntax error just prior to whatever included the file with the actual error.
So, have a look in the file that (indirectly) included _types.h. You likely have an unbalanced (.
Usually these cryptic errors in system header files result from your having other code (perhaps in another header file which was #import'ed earlier) that is missing a closing parenthesis. Often whenever you get strange compile errors, you have to look at the lines of code that the compiler encountered before the reported line and see if that earlier code was properly terminated.

What's the -Wsomething flag for 'instance method not found' warnings?

I recently had a case where someone added a parameter to an init method and broke another project that shared the code. Since it's only a warning, nobody realized the app was broken, so I'm trying to turn only this warning into an error:
warning: instance method '-someMethod' not found (return type defaults to 'id')
I've found that you can pass -Werror=foo in Other C Flags to the compiler in Xcode to turn a warning into the error, but I can't seem to find what 'foo' should be. I've tried 'undeclared-selectors' but that only catches #selector cases. I've tried -Werror-implicit-function-declaration but that doesn't seem to catch that case either.
I tried 'inst-method-not-found' and 'instance-method-not-found' after finding 'warn_inst_method_not_found' during a casual search of the huge clang source code.
Help ... ?
Update:
Here's an example you can compile (e.g. in CodeRunner) to see the warning: https://gist.github.com/4045701
The option you're looking for is -Werror=objc-method-access. Clang explicitly tells you this right in the warning message, if you download and compile that gist you posted:
% clang test.m -c
test.m:13:21: warning: instance method '-initWithNum:' not found (return type
defaults to 'id') [-Wobjc-method-access]
...theObj = [[MyClass alloc] initWithNum: [NSNumber numberWithInt: 15]];
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
% clang test.m -Werror=objc-method-access -c // ta-da!
But in real-world situations I agree with all the commenters above: You should fix or suppress all compiler warnings. Your build should build cleanly all the time. Otherwise, as you so rightly observed, you won't be able to distinguish real bugs from "the usual spam".
FWIW, here's the version of Clang I'm using:
$ clang --version
clang version 3.2 (http://llvm.org/git/llvm 1503aba4a036f5394c7983417bc1e64613b2fc77)
Target: x86_64-apple-darwin12.2.0
Thread model: posix

what is this error message complaining about? and how to resolve it?

try to apply a patch and compile received warning message:
<some_file>: In function '-[XXXXX saveAndCreate:]':
<some_file>:262: warning: 'NSString' may not respond to '-stringByReplacingOccurrencesOfString:withString:'
<some_file>:262: warning: (Messages without a matching method signature
<some_file>:262: warning: will be assumed to return 'id' and accept
<some_file>:262: warning: '...' as arguments.)
distcc[6493] ERROR: compile (null) on localhost failed
which is complaining about this code,
[clientHost findAvailableIn:
[clientHost defaultVMPath]
baseName:
[displayName stringByAppendingPathExtension:PL_BUNDLE_EXTENSION]
nameFormat:[[displayName
stringByReplacingOccurrencesOfString:#"%"
withString:#"%%"]
stringByAppendingString:#" %u.xxxx"]
startIndex:2
contextInfo:contextInfo];
and here is part of the compile command:
lin32/gcc-5363-1/bin/i686-apple-darwin8-gcc -mmacosx-version-min=10.4 -arch i386 -march=pentium-m -mtune=prescott -Werror -Wall -Wno-trigraphs -Wreturn-type -Wunused-variable -pipe -O2 -g3 -gfull -Wno-pointer-sign -x objective-c -fobjc-exceptions -fpascal-strings -fmessage-length=0
googled the warning and tried the method here , but still got warning message alike.
i am ignorant in Mac development, spare me if this is something obvious (and i donnot know if the information provide above is surfficent, let me know what else needed to provide). thanx.
edit:
1) extend the code block.
2) correct the warning message.
The warning is referencing a different line the one you cited. The message you use is called stringByReplacingOccurrencesOfString:withString: which is fine on NSString. However the method replaceOccurrencesOfString:withString attempts to change the receiver. NSString is immutable and can therefore not be changed. If you want to use that method send it to a NSMutableString object.
Are you importing <Foundation/Foundation.h>? What is very surprising is that you would receive this warning, but no warning on -stringByAppendingPathExtension. Is this really the only warning you're receiving?
I recommend breaking up the code to create some temporary variables. You're doing so many nested method calls, it makes it very hard to be certain where your errors are. The compiler will optimize out most temporary variables, so there's no reason to avoid them when it makes the code clearer.