LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Installing Kernel Header Files (https://www.linuxquestions.org/questions/linux-newbie-8/installing-kernel-header-files-788842/)

ananth86coolguy 02-13-2010 01:48 AM

Installing Kernel Header Files
 
Hi all
I am using Ubuntu 9.04 with kernel version 2.6.28-11-generic.I want to compile the foll. file but i get <linux/slab.h> no such file or directory.

mem_alloc.c

#include<linux/slab.h>
int main()
{
void * ptr;
ptr=kmalloc(1024,GPF_KERNEL);
return 0;
}

The kernel headers are installed in my system at /usr/src/linux-headers-2.6.28-11-generic/ directory. During compilation i specified as follows:

gcc -I/usr/src/linux-headers-2.6.28-11-generic/include mem_alloc.c

So can you tell me what might be the problem why gcc is unable to find the linux/slab.h header file?

Thanks & Regards
Ananth

irmin 02-13-2010 06:55 PM

If the file /usr/src/linux-headers-2.6.28-11-generic/include/linux/slab.h or /usr/include/linux/slab.h exist, the error can be a bug in gcc/cpp, which is unprobable. Thus please check if one of the above files really exists and is readable.

I do not understand the meaning of your program. kmalloc() is a kernel function that is not available in userspace (it allocates kernel memory). An equivalent replacement is the function malloc() from malloc.h

If you want to write a kernel module, then using a main() function is the wrong way to start with. In this case you should use:

Code:

#include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/slab.h>

  static int __init mymod_init(void)
  {
        void * ptr = kmalloc(1024,GPF_KERNEL);
        printk(KERN_INFO "kernel memory at %p\n", ptr);
        kfree(ptr);
        return 0;
  }

  module_init(mymod_init);



All times are GMT -5. The time now is 06:43 AM.