1

I have a struct abc in one file

struct abc {
    some variaables
    and functions
}

I am using this struct in other file as follows : struct abc *t = kmalloc(sizeof(struct abc)); kmalloc is equivalent to malloc

then following errors occur:

expected '=', ',', ';', 'asm' or '__attribute__' before 'struct'
error: variable 't' has initializer but incomplete type
warning: implicit declaration of function 'kmalloc'
invalid application of 'sizeof' to incomplete type 'struct trapframe'
storage size of 't' isn't known

where am I going wrong?

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
user1079065
  • 2,085
  • 9
  • 30
  • 53
  • kernel space or user space ? Kmalloc is a kernel-space function – brokenfoot Apr 07 '14 at 00:10
  • Ah, your locale is setup incorrectly. Modify your `.bashrc` to include `export LC_ALL=C` or something along those lines (I forgot what exactly) and you'll get more meaningful error messages instead of the â business. – Anish Ramaswamy Apr 07 '14 at 00:11
  • @brokenfoot -- kernel space function – user1079065 Apr 07 '14 at 00:27
  • 1
    maybe you forgot to include the `.h` file where the function is declared, somethin like `# include "include/linux/slab.h"` – jev Apr 07 '14 at 00:27
  • 1
    The error message indicates that `struct abc` is unrecognized, so you probably forgot to include that header or made a typo such as missing a semicolon. It also indicates that you didn't include the header for `kmalloc`. Further, `struct trapframe` is mentioned, which does not appear in your code. My conclusion - your actual code bears little resemblance to what you actually posted. Please post your exact real code next time! (Preferably copy-paste it in). – M.M Apr 07 '14 at 01:47

2 Answers2

0

Forgetting about the fact that you are using kmalloc instead of malloc for whatever reason, you can't use sizeof(struct abc) when in the current processing file you don't know the size of abc struct. Either declare abc struct in a header file, and then include that in your current file, or declare/define the struct in your current file... The compiler needs to know the size of the object you want to allocate space for, a forward declaration is not enough.

Frustrated Soul
  • 341
  • 3
  • 11
0

1, 2, 4 and 5 errors are caused by missing ; at the end of your struct declaration. It must be:

struct abc { some variaables and functions };

3 error is caused by missing the including of include/linux/slab.h file. You have to add the below file at the head of your source code file:

#include < linux/slab.h> # please remove the space before "linux"

vad
  • 344
  • 2
  • 12