xcode 为什么我在这个小样本中得到“没有以前的函数原型”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7297808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get "no previous prototype for function" in this little sample?
提问by TalkingCode
I have a very simple Objective-C sample
我有一个非常简单的 Objective-C 示例
#import <Foundation/Foundation.h>
int littleFunction();
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool
= [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}
int littleFunction()
{
return 0;
}
With this code I get a "no previous prototype for function" warning for littleFunction but as you can all see there is a declaration before main. What is wrong here? It seem the compiler is unable to match the declaration with the function implementation.
通过这段代码,我收到了 littleFunction 的“没有以前的函数原型”警告,但正如你们所见,在 main 之前有一个声明。这里有什么问题?编译器似乎无法将声明与函数实现相匹配。
If I change both like this:
如果我像这样改变两者:
int littleFunction(void)
it works perfectly. I am using the latest Xcode 4
它完美地工作。我正在使用最新的 Xcode 4
回答by Mat
In C, int littleFunction();
is not reallya prototype. It doesn't specify how many (or what sort of) parameters the function accepts.
在 C 中,int littleFunction();
并不是真正的原型。它没有指定函数接受多少(或什么样的)参数。
The actual wording in C99 is in §6.7.5.3 item 14:
C99 中的实际措辞在 §6.7.5.3 第 14 项中:
An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty listin a function declarator that is not part of a definition of that functionspecifies that no information about the number or types of the parameters is supplied.124
标识符列表仅声明函数参数的标识符。作为该函数定义的一部分的函数声明符中的空列表指定该函数没有参数。不属于该函数定义的函数声明符中的空列表指定不提供有关参数数量或类型的信息。124
The footnote refers to this section in the Future language directions:
脚注指的是未来语言方向中的这一部分:
6.11.6 Function declarators
The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.
6.11.6 函数声明符
使用带空括号的函数声明符(不是原型格式参数类型声明符)是一个过时的特性。
(Note: this is still present in the C11 draft I have (n1570).)
(注意:这仍然存在于我拥有的 C11 草案中(n1570)。)
Back in §6.7.5.3, item 10:
回到第 6.7.5.3 节,第 10 项:
The special case of an unnamed parameter of type
void
as the only item in the list specifies that the function has no parameters.
void
作为列表中唯一项的未命名参数类型的特殊情况指定该函数没有参数。
So you have to explicitly specify int foo(void);
if you want to prototype a function that doesn't take parameters. Objective-C has the same rules.
因此,您必须明确指定int foo(void);
是否要对不带参数的函数进行原型设计。Objective-C 也有同样的规则。
回答by basicthinker
I think the problem is that you did not use "static" for a globalfunction.
我认为问题在于您没有对全局函数使用“静态” 。
Please refer to the following:
请参考以下内容:
no previous prototype for `foo'
`foo' 没有以前的原型
This means that GCC found a global function definition without seeing a prototype for the function. If a function is used in more than one file, there should be a prototype for it in a header file somewhere. This keeps functions and their uses from getting out of sync
这意味着 GCC 在没有看到函数原型的情况下找到了一个全局函数定义。如果一个函数在多个文件中使用,那么应该在某个头文件中的某个地方有它的原型。这可以防止函数及其用途不同步
If the function is only used in this file, make it staticto guarantee that it'll never be used outside this file and document that it's a local function
如果该函数仅在此文件中使用,请将其设为静态以确保永远不会在此文件之外使用它并记录它是本地函数