Compiling a Linux kernel module using gcc with kernel header files

I have a Linux machine with kernel header files. I want to compile a C program using GCC with kernel A, while kernel B is currently running.

How can i do this? How to check that it works?

+4
source share
2 answers

This is additional information to learn. Post 2.6, as mentioned in another answer, the Makefile will take care of most of the steps involved in compiling a Linux kernel module. However, this is still based on GCC, and so it goes: (you can compile it without a Makefile too)

The following GCC parameters are required:

  • -/lib/modules/`uname -r`/build/include: , . default/usr/include/linux .

  • -D__KERNEL __: , , .

  • -DMODULE: .

gcc -DMODULE -D__KERNEL__ -isystem /lib/modules/$(uname -r)/build/include -c hello.c -o hello.ko

+5

, โ€‹โ€‹Makefile . make:

make -C $(KERNEL_SOURCE_DIR) M=`pwd` modules

Makefile :

KERNEL_DIR := /lib/modules/$(shell uname -r)/build

obj-m := test.o

driver:
    make -C $(KERNEL_DIR) M=`pwd` modules

clean:
    make -C $(KERNEL_DIR) M=`pwd` clean

KERNEL_DIR /lib/modules/$(shell uname -r)/build, , . , .

, , gcc.

+3

All Articles