Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux/module.h: No such file or directory

i'm a beginner and i'm trying out some basics of kernel programming in linux. Today morning i've opened the module.h file in VIM, and closed without saving any changes as well. After that i'm not able to compile any of my codes. I'm getting the following error message

[root@localhost helloworld]# cc helloworld.c
helloworld.c:1:25: error: linux/module.h: No such file or directory
[root@localhost helloworld]# 

Here is a sample code which was running successfully till the last day.

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

int init_module(void)
{
        printk("HELLO WORLD");
        return 0;
}

void cleanup_module(void)
{
        printk("GOODBYE");
}

I searched for the module.h file like the following and it do exist

[root@localhost usr]# find . -name module.h
./src/kernels/2.6.18-194.el5-i686/include/asm-x86_64/module.h
./src/kernels/2.6.18-194.el5-i686/include/asm-i386/module.h
./src/kernels/2.6.18-194.el5-i686/include/linux/module.h
./include/sepol/policydb/module.h
./include/sepol/module.h
./include/kde/kunittest/module.h
[root@localhost usr]# 

Please help me out. I'm using CentOS in virtual box.

like image 921
ajishalfred Avatar asked Nov 09 '11 09:11

ajishalfred


People also ask

Where is linux module H?

h . That file is provided by kernel header packages, e.g. on Debian derivatives, the linux-headers-$(uname -r) package. The /usr/include/linux/kernel. h is intended for user processes, not for kernel modules.

What does linux init h do?

These macros are defined in linux/init. h and serve to free up kernel memory. When you boot your kernel and see something like Freeing unused kernel memory: 236k freed, this is precisely what the kernel is freeing. This macro served the same purpose as __init, but is now very deprecated in favor of __init.

How do I build a Linux kernel from module H?

There ARE some linux headers there (/usr/include/linux), but module.h is not one of them, as it deals with kernel constructs directly. In short, you need a Makefile. Also, get rid of the #define MODULE. save the following to a file called Makefile and run make: This triggers the kernel build system.

Why does this file not exist in Linux?

This file does not exist, because in Linux, by default you are developing a user space application. These have very limited access to any system-wide interrupts. The file in your /usr/src/linux*/include/linux, is a kernel header. It is not usable by user-space apps. It is usable only for kernel modules.

What is the difference between /usr/include/Linux and /path-to-kernel-source/ include/Linux?

In short, /usr/include/linux contains the headers for the user-space processes to deal with the kernel API. While the /path-to-kernel-source/include/linux contains the internal header files used by the kernel. Linux kernel developers face them mostly if he develops kernel modules.

Where do I find the build directory for Linux kernel?

You need the kernel build environment (selection of scripts, header and Makefiles) usually this is reachable through /lib/modules/ version /build (a symlink to it) if a kernel has been installed already. Otherwise, the directory is the build directory (the one where System.map is in).


1 Answers

You're trying to compile your module with plain gcc with none of the surrounding kbuild framework. You might have gotten something to work in the past with this approach, but it is painful horrible awful to try to maintain a module using anything other than pure-kbuild Makefile approaches. I've wasted too much of my life fighting against kbuild and I don't want the same to happen with you -- embrace kbuild and let it help you build your module. Please read Documentation/kbuild/modules.txt before writing another line of code.

What you need to do is create a Makefile for your module. Its contents should look like this:

ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
obj-m  := modulename.o

else
# normal makefile
    KDIR ?= /lib/modules/`uname -r`/build

default:
$(MAKE) -C $(KDIR) M=$$PWD

endif

I know it's a lot more complicated than most Makefiles you're used to seeing, but it serves a dual-purpose. If you just run make in your directory, it'll re-invoke make to use the kbuild mechanism from the currently-running kernel (assumed to at least have a symlink from /lib/modules/.../build to the correct location).

The re-invoked make command ($(MAKE)) will properly build your module and save you more time than you can ever appreciate. (Really.)

Keep Documentation/kbuild/modules.txt by your side while making this work.

Note: Documentation/kbuild/modules.txt may be available at your linux system at /usr/share/linux-headers-$(uname -r)/Documentation/kbuild/modules.txt

like image 117
sarnold Avatar answered Oct 26 '22 06:10

sarnold