开发者

How to handle the Xcode warning "no previous prototype for function..."?

This warning is popping up a bunch in some third party libraries.

Is there a way to handle it without modifying the code (e.g. ignore the warning)?

If I have to modify the code to fix it how do I do it?

Here's one of the code blocks that's causing a warning:

BOOL FBIsDeviceIPad() {
 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
  if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
   return YES;
  }
 #endif
  return NO;开发者_Go百科
}


Usually with warnings like this you can just define a function prototype at the top of your file, for instance:

BOOL FBIsDeviceIPad();

But in C a method with nothing between the braces, i.e. () actually implies there are an arbitrary number of parameters. Instead the definition should become (void) to denote no parameters:

BOOL FBIsDeviceIPad(void);

...

BOOL FBIsDeviceIPad(void) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
  if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
   return YES;
  }
#endif
  return NO;
}


In Xcode4, go to your project's Build Settings. Search for "prototype". There should be an option called "Missing Function Prototypes"; disable it. You can also do this to the specific target(s) in question.


There are no warnings if such a function is defined as inline.

This may suffice as long as your function is optimized for inline use. http://msdn.microsoft.com/en-us/library/1w2887zk.aspx


Another way of fixing the warning is to make the functions static so they're only visible within the file (specifically, can only be linked within the file's translation unit).

It seems like part of the use of this warning is that if you have a standalone function, you either want it usable in other implementation files, or only in the file where it's defined. This warning makes you be explicit about that choice, and helps you be aware if the header has diverged from the implementation.

If you want it usable in other implementation files, you should have its prototype in a header somewhere.

If you want it only usable in this file, then it should be static.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜