[英]error: conflicting types for 'f' and previous declaration of 'f' was here
This code is just a situation that I found in my actual code which is very big so I'm giving this. 这段代码只是我在实际代码中发现的一个非常大的情况,所以我给出了这个。 here in this code the structure "struct node" is not defined it is defined in another c source file.
在此代码中,结构“struct node”未定义,它在另一个c源文件中定义。
my c source code: 我的源代码:
/* test.c */
1 #include<stdio.h>
2 #include "test2.h"
3
4 void f(struct node * k)
5 {
6
7 }
my header file : 我的头文件:
/* test2.h */
1 extern void f(struct node * k);
When I compile this code with gcc to create an object file: 当我用gcc编译这段代码来创建一个目标文件时:
gcc -w -c test.c
I get: 我明白了:
test.c:6: error: conflicting types for 'f'
test2.h:1: error: previous declaration of 'f' was here
I have given the complete prototype of function f()
. 我已经给出了函数
f()
的完整原型。 Why I am getting this error? 为什么我收到此错误?
Another thing is that when I don't include the header file test2.h in test.c and explicitly declare the function prototype in test.c
, it compiles successfully. 另一件事是,当我不包括在test.c的头文件test2.h和显式声明函数原型
test.c
,它编译成功。 Code is below: 代码如下:
/* test.c */
1 #include<stdio.h>
2 void f(struct node *k);
3
4 void f(struct node * k)
5 {
6
7 }
gcc -c -w test.c gcc -c -w test.c
No error. 没错。
Can you please explain why this time I am not getting an error? 你能解释为什么这次我没有收到错误吗?
This has nothing to do with the extern
keyword on the prototype (although it's not necessary). 这与原型上的
extern
关键字无关(尽管没有必要)。
You need a forward declaration for struct node
: 您需要
struct node
的前向声明:
/* test2.h */
struct node; // <== this
extern void f(struct node * k);
Without that forward declaration, the struct node
inside the function prototype is local to that particular function prototype. 如果没有前向声明,函数原型中的
struct node
对于该特定函数原型是本地的 。
So when the compiler sees the definition of f()
it also sees another different local declaration for a struct node
and thinks that you have a conflicting declaration for f()
. 因此,当编译器看到
f()
的定义时,它还会看到struct node
另一个不同的本地声明,并认为你对f()
有一个冲突的声明。
See C99 6.9.1 "Scopes of identifiers": 见C99 6.9.1“标识符范围”:
For each different entity that an identifier designates, the identifier is visible (ie, can be used) only within a region of program text called its scope.
对于标识符指定的每个不同实体,标识符仅在称为其范围的程序文本的区域内可见(即,可以使用)。 Different entities designated by the same identifier either have different scopes, or are in different name spaces.
由相同标识符指定的不同实体具有不同的范围,或者在不同的名称空间中。 There are four kinds of scopes: function, file, block, and function prototype.
范围有四种:函数,文件,块和函数原型。 (A function prototype is a declaration of a function that declares the types of its parameters.)
(函数原型是声明其参数类型的函数的声明。)
...
...
If the declarator or type specifier that declares the identifier appears within the list of parameter declarations in a function prototype (not part of a function definition), the identifier has function prototype scope, which terminates at the end of the function declarator.
如果声明标识符的声明符或类型说明符出现在函数原型(不是函数定义的一部分)的参数声明列表中,则标识符具有函数原型作用域,该作用域终止于函数声明符的末尾。
GCC 4.6.1 gives a nice warning about this for me: GCC 4.6.1为我提供了一个很好的警告:
C:\temp\test.c:3:22: warning: 'struct node' declared inside parameter list [enabled by default]
C:\temp\test.c:3:22: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]
C++ makes function prototype scope apply only to the parameter names (C++03 3.3.3), so you won't see this problem if you compile the code as C++. C ++使函数原型范围仅适用于参数名称(C ++ 03 3.3.3),因此如果将代码编译为C ++,则不会看到此问题。
remove the extern from the header. 从标题中删除extern。 When you declare a function as external, you guide the compiler that the function is not going to be compiled, and any reference to that function will be resolved in the linkage.
当您将函数声明为外部函数时,您将指导编译器不会编译该函数,并且将在链接中解析对该函数的任何引用。 Usually, this declaration is done when working with pre compiled libraries.
通常,此声明在使用预编译库时完成。
In your test2.h you have declared f
as extern
, that's why you are getting the error. 在你的test2.h中你已经将
f
声明为extern
,这就是你得到错误的原因。 In test.c there is no extern
declaration in prototype. 在test.c中,原型中没有
extern
声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.