Compile Linux Kernel (2.6) Module Including Non Kernel Headers

Compile linux kernel (2.6) module including non kernel headers

The kernel cannot use userspace code and must stand alone (i.e. be completely self contained, no libraries), therefore it does not pick up standard headers.

It is not clear what benefit trying to pick up userspace headers is. If there are things in there that it would be valid to use (constants, some macros perhaps provided they don't call any userspace functions), then it may be better to duplicate them and include only the kernel-compatible parts that you need.

It is not possible to link the kernel with libraries designed for userspace use - even if they don't make any OS calls - because the linking environment in the kernel cannot pick them up.

Instead, recompile any functions to be used in the kernel (assuming they don't make any OS or library calls - e.g. malloc - in which case they'll need to be modified anyway). Incorporate them into your own library to be used in your kernel modules.


Recent versions of linux contain cryptographic functions anyway, including various SHA hashes - perhaps you can use one of those instead.


Another idea would be to stop trying to do crypto in kernel-space and move the code to userspace. Userspace code is easier to write / debug / maintain etc.

Compile a linux 2.6 kernel module with newer compiler

Your kernel version is too old and it is missing linux/compiler-gcc5.h.

The header is included from linux/compiler-gcc.h and its name is generated by preprocessor macro based on current compiler version:

#define _gcc_header(x) __gcc_header(linux/compiler-gcc##x.h)

This header was introduced around version 3.18.

You might try to get this file from a newer kernel source and put in into include/linux.

Compiling Linux kernel module using gcc with kernel header files

This is additional info to delve into. Post 2.6 version, as mentioned in other reply the Makefile takes care of most of the Linux kernel module compilation steps. However, at the core of it is still GCC, and this is how it does: (you too may compile it without Makefile)

Following GCC options are necessary:

  1. -isystem /lib/modules/`uname -r`/build/include: You must use the kernel headers of the kernel you're compiling against. Using the
    default /usr/include/linux won't work.

  2. -D__KERNEL__: Defining this symbol tells the header files that the code will be run in kernel mode, not as a user process.

  3. -DMODULE: This symbol tells the header files to give the appropriate definitions for a kernel module.

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

How to extract kernel headers for compiling kernel module later

Create kernel packages instead, that's "make deb-pkg" for dpkg based distros and "make rpm-pkg" for RPM based ones. These create multiple packages, one of those is a package usable for external modules building. That should be linux-headers-* for the Debian packages and a "devel" package for he RPM versions.



Related Topics



Leave a reply



Submit