2

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.

1
  • 1
    naming the variable the same as the struct is a very bad programming practice and will result is massive errors when performing maintenance some 6 months (or years) from now. Even this early, you are getting burned from making this naming error. Commented Oct 28, 2014 at 1:04

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I don't know how to achieve the above thing using something-else. And yes, I've never used kmalloc outside a function before. This is the first time I'm trying.
That is ... interesting reasoning, since what you've done isn't valid, and thus doesn't achieve anything. :) What are you trying to achieve?
hello is actually a linked list (sorry for not mentioning that too). Whenever I try to assign something to hello, I check if(hello==NULL) then create 2 nodes for the linked list including the root node. else create only one node and add it to the linked list.
@RatDon I wasn't very clear, sorry. I'll try again: what are you trying to achieve by putting that code outside of any function? Is this some kind of initialization?
C won't allow it just because you are trying it for the first time ;-) Not sure if you are ready to play with kernel code before fully grasping basics of C.
|
3

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.

8 Comments

I've just mentioned a sample code regarding my problem. Rest all header files are there. And the whole code is in kernel. So instead of malloc(), I'm using kmalloc().
@RatDon Do you expect us to magically know things you're not including in your question?
You can't use user space headers in kernel code. If it's just an example, I assume you haven't included <linux/slab.h> which has prototype for kmalloc().
@BlueMoon Updated the question with the exact sample of my code.
You can't have statements (except declarations) outside of a function's body. Wrap your code in a function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.