0

Currently working on Rpi-zero-w.

I tried to compile driver code, and I'm missing some files:

#include <linux/clk.h>
#include <linux/completion.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>

All those are missing from /usr/include/linux

I googled it and I'm lost, do I need to recompile the kernel headers to get them ? if yes here is the error when I try to recompile kernel headers:

how to reproduce, create test.c containing previous headers and use following makefile:

# obj-m is a list of what kernel modules to build.  The .o and other
# objects will be automatically built from the corresponding .c file -
# no need to list the source files explicitly.

obj-m := test.o

# KDIR is the location of the kernel source.  The current standard is
# to link to the associated source tree from the directory containing
# the compiled modules.

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

# PWD is the current working directory and the location of our module
# source files.

PWD   := $(shell pwd)

# default is the default make target.  The rule here says to run make
# with a working directory of the directory containing the kernel
# source and compile only the modules in the PWD (local) directory.

default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
2
  • You'll probably want to apt-get the appropriate package(s) for compiling drivers - it may be raspberrypi-kernel-headers but I'm not sure? Commented May 14, 2018 at 9:47
  • @JaromandaX I did, still missimg the files in /lib/include/linux/
    – A.albin
    Commented May 14, 2018 at 11:58

1 Answer 1

1

The files in kernel-headers packages are for compiling userspace applications; they're e.g., part of the syscall interface.

To compile a kernel module you need to compile it against a kernel source, and you will find all those files in <src>/include/linux. You can get the source for the Pi kernel from the top level of that github repo. You must make sure you use the exact same version as the kernel you are using; check uname -r, and the source version is at the top of the top level Makefile (e.g., the one linked above is 4.14.39).

Or..

Normally on linux distros the kernel source is available in a package; on Debian this is called linux-source. I do not have a Raspbian system running, so I cannot check this, but have a look at the output of:

apt search linux-source

There will be at least one package, but most of them are probably from Debian, not Raspbian, and are of no use on the pi. Look again at the output from uname -r. If there is a version number that matches, try installing that. Then go to the /lib/modules/x.x.x directory that exactly matches, e.g., if uname -r gives you 4.14.39-v7+, you want to look in /lib/modules/4.14.39-v7+. Then try and look in the build directory there. If it is not actually a directory, but a dangling symlink, you will have to install the source from github as already mentioned above. If it is a directory with lots of stuff in it, you are in luck


If you downloaded the source from github, put it in usr/src and symlink it to /lib/modules/$(shell uname -r)/build, which is mentioned in your module makefile.

srcv=$(uname -r)
sudo rm /lib/modules/$srcv/build
sudo ln -s /usr/src/$srcv /lib/modules/$srcv/build

You should also put the kernel .config in that source directory. You can get it by:

sudo modprobe configs
cp /proc/config.gz .
gunzip config.gz
mv config .config

This should give you a decent chance of getting a module that works.

5
  • Ok, I did install the linux header & run 'make modules_prepare' but still miss those files...
    – A.albin
    Commented May 15, 2018 at 0:04
  • (ran make too )
    – A.albin
    Commented May 15, 2018 at 0:11
  • My point is those files are not in the linux-headers package. In Raspbian, as far as I am aware, they are not in any package (however, I have added a note about this above under "Or..."). They are in the kernel source. The linux-headers package is not the kernel source. You need to read a real tutorial or book on making linux modules or it is going to be one headache after another for you.
    – goldilocks
    Commented May 15, 2018 at 12:13
  • linux-header package on Rasbian does contains the sources, or at least also pushes the sources in /usr/src/, I did build, but something went wrong on the first Pi-zero, the second one did get the includes files after build.
    – A.albin
    Commented May 15, 2018 at 14:31
  • 1
    Okay; there's some confusion because kernel-headers, linux-headers, and linux-source are three different packages. I should not have said linux-headers in my last comment, I meant kernel-headers, which I used in the answer and is referred to in comments on your question. Anyway, I hope you have got things working now.
    – goldilocks
    Commented May 15, 2018 at 16:21

Your Answer

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

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