Search code examples
cstructkernelglobal-variablesextern

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.


Solution

  • 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.