RatDon
RatDon

Reputation: 3543

struct definition in header file: type defaults to 'int'

I've a structure declaration and definition in header file header.h as:

#include <linux/slab.h>

struct hello{
    int a;
    char b;
};

extern struct hello *hello;

In file1.c I've:

#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:

#include<header.h>

The struct variable hello is used in file1.c and file2.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.

Upvotes: 2

Views: 1420

Answers (2)

unwind
unwind

Reputation: 400019

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.

Upvotes: 3

P.P
P.P

Reputation: 121407

You forgot to include <stdlib.h> and the compiler assumes int as default return value for malloc().

The default implicit int has been removed since C99. In any case, you should always include necessary hesders to get correct prototypes.

Upvotes: 3

Related Questions