如果我错过了原型(prototype),XCode (LLVM) 会提示我出错
exceptionHandler 的函数没有以前的原型(prototype)
但为什么我下面的代码中需要它们?
void exceptionHandler(NSException * exception); // Why this Line is needed?
void exceptionHandler(NSException * exception)
{
// ....
}
@implementation AppDelegate
- (void) applicationDidFinishLaunching:(UIApplication *)application
{
NSSetUncaughtExceptionHandler(&exceptionHandler);
...
最佳答案
来自 GCC 手册:
-Wmissing-prototypes (C and Objective-C only)
Warn if a global function is defined without a previous prototype declaration. This warning is issued even if the definition itself provides a prototype. The aim is to detect global functions that fail to be declared in header files.
Clang 借用了此选项以实现 GCC 兼容性,并且因为它很有用(我认为这是 Clang 开发人员的用处)。
该选项的存在是为了防止您犯下本可以轻松避免的常见错误。为了清楚/意图的缘故,明确说明可见性/链接是很好的。
简而言之,您通过启用此选项要求编译器在非限定定义与声明不匹配时告诉您。您应该将其限定为 extern
并使其可供其他人使用(例如,将其放在 header 中),或者将其声明为 static
。如果使用 C++ inline
也是一种选择。
当然,隐式可见性是众所周知的,但我通常发现该选项在这些场景中很有用:
1) 我打错了:
// file.h
extern void MONExceptionHandler(NSException * exception);
和
// file.m
void MONExceptionhandler(NSException * exception) {
…
2) 我应该明确说明符号的可见性:
// file.m
static void MONExceptionHandler(NSException * exception) {
…
3) 我忘记了 #include
声明函数的 header :
// file.h
extern void MONExceptionHandler(NSException * exception);
警告:
// file.m
void MONExceptionHandler(NSException * exception) {
…
无警告:
// file.m
#include "file.h"
void MONExceptionHandler(NSException * exception) {
…
所以这里有基本原理、历史和一些示例 - 同样,-Wmissing-prototypes
是一个选项。如果您相信自己可以在禁用它的情况下工作,那么就这样做吧。我的偏好是明确的,让程序检测潜在和实际的问题,这样我就不必手动完成。
关于iphone - 奇怪的 LLVM 警告 : no previous prototype for function for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8860501/