2

I'm trying to setup an environment for kernel module development in Linux. I've built the kernel in the home folder and would like to place the sources and binaries to the correct place so include correctly.

The example for building the kernel module has the following includes:

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

What are the absolute paths that the linker looks for these headers?

3 Answers3

2

I generally approach this question like this. I'm on a Fedora 19 system but this will work on any distro that provides locate services.

$ locate "linux/init.h" | grep include
/usr/src/kernels/3.13.6-100.fc19.x86_64.debug/include/linux/init.h
/usr/src/kernels/3.13.7-100.fc19.x86_64.debug/include/linux/init.h
/usr/src/kernels/3.13.9-100.fc19.x86_64/include/linux/init.h
/usr/src/kernels/3.13.9-100.fc19.x86_64.debug/include/linux/init.h

Your paths will be different but the key take away is that you want to ask locate to find what's being included ("linux/init.h") and filter these results looking for the keyword include.

There are also distro specific ways to search for these locations using RPM (Redhat) or APT (Debian/Ubuntu).

gcc

Notice however that the paths within the C/C++ file are relative:

#include <linux/init.h>

This is so that when you call the compiler, gcc, you can override the location of the include files that you'd like to use. This is controlled through the switch -I <dir>.

excerpt from man gcc

   -I dir
        Add the directory dir to the list of directories to be searched for 
        header files.  Directories named by -I are searched before the 
        standard system include directories.  If the directory dir is a
        standard system include directory, the option is ignored to ensure 
        that the default search order for system directories and the special 
        treatment of system headers are not defeated .  If dir
        begins with "=", then the "=" will be replaced by the sysroot 
        prefix; see --sysroot and -isysroot.

External modules

There's this article which discusses how one would incorporate the development of their own kernel modules into the "build environment" that's included with the Linux kernel. The article is titled: Driver porting: compiling external modules. The organization of the Kernel's makefile is also covered in this article: makefiles.txt.

For Kernel newbies there's also this article: KernelHeaders from the kernelnewbies.org website.

NOTE: The Kernel uses the KBuild system which is covered here as part of the documentation included with the Kernel.

References

slm
  • 369,824
1

Answers are often distribution specific, because they may have specific machinery to do this. In the case of Debian there is The Debian Linux Kernel Handbook. Since Ubuntu is essentially Debian, all of this should apply. If I understand correctly, you are asking about installing the kernel. One approach, a good one, is to build a binary package for the kernel and kernel headers, and install those, and this handbook shows you how. See also my answer to "How do I upgrade the Debian Wheezy kernel offline?". This answer really could do with some cleaning up.

Note that using the stock Debian/Ubuntu kernel and kernel header binary packages is an entirely reasonable thing to do, and does not require an compilation.

Faheem Mitha
  • 35,108
1

The kernel has a deb-pkg target. On debian, make deb-pkg and sudo dpkg -i'ing the two resulting packages (in ../) worked like a charm for me. I'd think it works the same on Ubuntu. On build and install, those includes wind up in the right places automagically.

jthill
  • 2,710