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?

5
  • What OS, please? Apr 20, 2014 at 11:45
  • @FaheemMitha Linux Apr 20, 2014 at 11:46
  • What linux distribution, please? Including version. Apr 20, 2014 at 11:47
  • @FaheemMitha Xubunt 13.10, kernel 3.14. If you know how to answer, please mention why is this issue distribution specific. Apr 20, 2014 at 11:53
  • If you keep it all in your home, there is no "correct place". You just give your chosen path to the build system. If you want to install them in /, then it's distro specific.
    – Leiaz
    Apr 20, 2014 at 12:47

3 Answers 3

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

1
  • I've listed the default include paths for gcc following this question but it doesn't include a path to the header files. I'm hoping to setup the things the default way to avoid the -I option in gcc. Apr 20, 2014 at 18:18
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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.