How to handle ‘No previous prototype for function’ warnings in Xcode

When compiling code in Xcode 4 you may be seeing warnings saying ‘No previous prototype for function…’.  This is probably happening if you are using a library that has at least some parts written in C/C++.  This warning may even be appearing in code that previously compiled fine in Xcode 3 without any warnings.  This is because Xcode 4’s default compiler warns about this when Xcode 3’s did not.

Anyway, the warning is simply saying that a function implementation exists but no matching function declaration was found.  If you are used to Objective-C only this is like saying that you have the implemented code in the .m file but you don’t have the function listed in the .h.  It’s easy to fix, just write the method declaration in anywhere before the actual method; you can write it in the .m or .h, it won’t matter as long as it’s before the actual function.

For example, if you have a method such as…

float doSimpleMaths(float a, float b) {
   return (a*a)*(b*b);
}

Then to get rid of the warning just add in the method declaration before the method…

float doSimpleMaths(float a, float b);
float doSimpleMaths(float a, float b) {
   return (a*a)*(b*b);
}