简体   繁体   English

头文件中的struct定义:类型默认为'int'

[英]struct definition in header file: type defaults to 'int'

I've a structure declaration and definition in header file header.h as: 我在头文件header.h具有结构声明和定义,如下所示:

#include <linux/slab.h>

struct hello{
    int a;
    char b;
};

extern struct hello *hello;

In file1.c I've: 在file1.c中,我已经:

#include<header.h>

struct hello *hello;
hello=kmalloc(sizeof(struct hello), __GFP_REPEAT);
kfree(hello);    //just to check later if 'hello' -
hello=NULL;      //-is initialized or not.

In file2.c I've: 在file2.c中,我已经:

#include<header.h>

The struct variable hello is used in file1.c and file2.c . 结构变量hellofile1.cfile2.c

But while compiling I get an error: 但是在编译时出现错误:

file1.c:3:1 error: type defaults to 'int' in declaration of 'hello' [-Werror=implicit-int]
file1.c:4:1 error: conflicting types for 'hello'
file1.c:3:16 note: previous declaration of 'hello' was here 
 extern struct hello *hello;

I've never used variable definition in header file. 我从未在头文件中使用变量定义。 Searched online and got this from few sources. 在线搜索,并从几个来源获得此信息。 Unable to find what is wrong. 无法找到问题所在。 A lot of other errors are there after this which originates due to the mentioned error. 此后,还有许多其他错误,这些错误是由于上述错误引起的。

Edited to include the proper codes. 编辑以包含适当的代码。

You forgot to include <stdlib.h> and the compiler assumes int as default return value for malloc() . 您忘记了包含<stdlib.h> ,并且编译器假定intmalloc()默认返回值。

The default implicit int has been removed since C99. 自C99以来,默认的implicit int已删除。 In any case, you should always include necessary hesders to get correct prototypes. 无论如何,您都应始终包括必要的赫德斯以获得正确的原型。

Is this: 这是:

hello=kmalloc(sizeof(struct hello), __GFP_REPEAT);

really at file-level scope like that? 真的在文件级范围内吗? You can't have code like that outside a function in C, but I would expect a different error message. 您不能在C中的函数外部拥有类似的代码,但是我希望得到不同的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM