gcc原型怪异 [英] gcc prototype oddity

查看:132
本文介绍了gcc原型怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Thomas Heinz写道(在这个错误的

计划的gcc汇编中):


$ cat test.c

int f(int);

int f();

int f(){return 0;}


int main(void){return 0; }

唯一的问题仍然是关于程序的错误陈述......这是:

test.c:在函数中? f?:
test.c:3:错误:参数数量与原型不匹配
test.c:2:错误:原型声明

这里,它会如果原型声明(
错误声明的第3行)引用最严格的声明(在
示例第1行中),那就太好了。




有趣的是:这条消息确实有误,因为第2行和第3行之间没有

不一致,只有1到3之间。


我'' m cc''ing comp.lang.c.

Allin Cottrell。

解决方案

cat test。 c

int f(int);

int f();

int f(){return 0;}


int main(void){return 0; }

唯一的问题仍然是关于程序的错误陈述......这是:

test.c:在函数中? f?:
test.c:3:错误:参数数量与原型不匹配
test.c:2:错误:原型声明

这里,它会如果原型声明(
错误声明的第3行)引用最严格的声明(在
示例第1行中),那就太好了。




有趣的是:这条消息确实有误,因为第2行和第3行之间没有

不一致,只有1到3之间。


我'' m cc''ing comp.lang.c.


Allin Cottrell。


Allin Cottrell写道:
< blockquote class =post_quotes> Thomas Heinz写道:


cat test.c
1 int f(int);
2 int f();
3 int f(){return 0; }
4
5 int main(void){return 0; }

唯一的问题仍然是关于程序的错误陈述......这是:

test.c:在函数中? f?:
test.c:3:错误:参数数量与原型不匹配
test.c:2:错误:原型声明

这里,它会如果原型声明(第3行的错误声明)引用最严格的声明(在示例第1行中),那就太好了。
有趣的是:该消息确实有误,
因为第2行和第3行之间没有不一致,只有1到3之间。




不,消息是正确的。

声明


int f();


重新声明


int f(int);


和编译器可以告诉的一样。

cat main.c
int f();

int f(int);

int f(){return 0; }


int main(void){return 0; }

gcc -Wall -std = c99 -pedantic -o main main.c



main.c:在函数`f'':

main.c:3:错误:参数数量与原型不匹配

main.c:2:错误:原型声明


编译器是*不*千里眼。

它不知道我做什么*打算*写。


由于类型声明符出现在声明中


int f(int);


错误可能是声明


int f(无效);


的目的是你写的地方


int f();


在这种情况下你也应该写好


int f(void){return 0; }


Thomas Heinz wrote (in re. gcc compilation of this erroneous
program):

$ cat test.c
int f(int);
int f();
int f() {return 0;}

int main (void) { return 0; }

The only point which remains concerns the error statement for
the program ... which is:

test.c: In function ?f?:
test.c:3: error: number of arguments doesn''t match prototype
test.c:2: error: prototype declaration

Here, it would be nice if the prototype declaration (3rd line of
error statement) refers to the strictest declaration (in the
example line 1).



Interesting: that message does seem wrong, since there''s no
inconsistency between lines 2 and 3, only between 1 and 3.

I''m cc''ing comp.lang.c.

Allin Cottrell.

解决方案

cat test.c
int f(int);
int f();
int f() {return 0;}

int main (void) { return 0; }

The only point which remains concerns the error statement for
the program ... which is:

test.c: In function ?f?:
test.c:3: error: number of arguments doesn''t match prototype
test.c:2: error: prototype declaration

Here, it would be nice if the prototype declaration (3rd line of
error statement) refers to the strictest declaration (in the
example line 1).



Interesting: that message does seem wrong, since there''s no
inconsistency between lines 2 and 3, only between 1 and 3.

I''m cc''ing comp.lang.c.

Allin Cottrell.


Allin Cottrell wrote:

Thomas Heinz wrote:


cat test.c
1 int f(int);
2 int f();
3 int f() { return 0; }
4
5 int main(void) { return 0; }

The only point which remains concerns the error statement for
the program ... which is:

test.c: In function ?f?:
test.c:3: error: number of arguments doesn''t match prototype
test.c:2: error: prototype declaration

Here, it would be nice if the prototype declaration
(3rd line of error statement) refers to
the strictest declaration (in the example line 1).
Interesting: that message does seem wrong,
since there''s no inconsistency
between lines 2 and 3, only between 1 and 3.



No, the message is correct.
The declaration

int f();

is a redeclaration of

int f(int);

and far as the compiler can tell.
cat main.c int f();
int f(int);
int f() { return 0; }

int main(void) { return 0; }
gcc -Wall -std=c99 -pedantic -o main main.c


main.c: In function `f'':
main.c:3: error: number of arguments doesn''t match prototype
main.c:2: error: prototype declaration

The compiler is *not* clairvoyant.
It does not know any more than I do what you *intended* to write.

Since the type specifier appears in the declaration

int f(int);

the mistake may be that the declaration

int f(void);

was intended where you wrote

int f();

in which case you should also have written

int f(void) { return 0; }


这篇关于gcc原型怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆