I am trying to learn how to write linux kernel modules on a raspberry pi 4.
I cannot find a necessary folder for compiling my .c to a .ko
The command I am trying to run in the Makefile is:
make -C /lib/modules/$(uname -r)/build M=$(PWD) modules
However, my $(uname -r) expands to 6.1.21-v8+ and there is no folder at /lib/modules/6.1.21-v8+/build
There are several other versions in my /lib/modules/ but they don't match $(uname -r) and don't work when used instead.
To setup my environment for programming I installed the raspberrypi-kernel-headers with
$ sudo apt install raspberrypi-kernel-headers
But the build folder doesn't come included with it.
Does anyone know where I can find the build folder or another way to fix this issue.
For context, the tutorial I am following is https://www.youtube.com/watch?v=4tgluSJ ... Ma&index=2
and he uses a raspberry pi 3 instead of a 4
Any help would be greatly appreciated!!
-
- Posts: 2
- Joined: Wed May 17, 2023 2:43 am
- DougieLawson
- Posts: 42639
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
Re: Cannot find build folder in /lib/module/$(name -r)/build folder for compiling linux kernel modules
Run https://github.com/RPi-Distro/rpi-source that gets the kernel source and sets up the /lib/modules/$(uname -r)/build folder.
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
-
- Posts: 2
- Joined: Wed May 17, 2023 2:43 am
Re: Cannot find build folder in /lib/module/$(name -r)/build folder for compiling linux kernel modules
SOLUTION
Thank you very much DougieLawson!
I followed that the instructions there but the root cause of the problem persisted to screw things up after using rpi-source.
I was able to use rpi-source to get the correct build folder but I ran into the same errors outlined in https://github.com/RPi-Distro/rpi-source/issues/14
Ultimately the issue was that I was using a headless 32bit OS instead of a headless 64bit OS. I switched to 64bit and everything worked and was where it needed to be after "sudo apt install rasberrypi-kernel-headers"!
I never would have realized the issue was due to this if I hadn't read the issue in the rpi-source repo, so DougieLawson, your assistance helped me down the path to a solution, thank you!
Thank you very much DougieLawson!
I followed that the instructions there but the root cause of the problem persisted to screw things up after using rpi-source.
I was able to use rpi-source to get the correct build folder but I ran into the same errors outlined in https://github.com/RPi-Distro/rpi-source/issues/14
Ultimately the issue was that I was using a headless 32bit OS instead of a headless 64bit OS. I switched to 64bit and everything worked and was where it needed to be after "sudo apt install rasberrypi-kernel-headers"!
I never would have realized the issue was due to this if I hadn't read the issue in the rpi-source repo, so DougieLawson, your assistance helped me down the path to a solution, thank you!
Re: Cannot find build folder in /lib/module/$(name -r)/build folder for compiling linux kernel modules
I did the following eight steps and ended up with the expected build folder created. Since I did not trace the /lib/modules//lib/modules/6.1.28-v7+/ change in the process, I do not know the step which creates the build folder. Probably you would like to make a comment.
1. tclee@raspberrypi:~/may24wed23 $ git clone https://github.com/RPi-Distro/rpi-source.git
......
2. tclee@raspberrypi:~/may24wed23 $ ls -lrtag
.....
4 drwxr-xr-x 4 tclee 4096 May 24 19:44 rpi-source
3. tclee@raspberrypi:~/may24wed23 $ cd rpi-source/
4. tclee@raspberrypi:~/may24wed23/rpi-source $ ls -lrt
total 36
-rwxr-xr-x 1 tclee tclee 14223 May 24 19:44 rpi-source
drwxr-xr-x 3 tclee tclee 4096 May 24 19:44 debian
-rw-r--r-- 1 tclee tclee 11828 May 24 19:44 README.md
-rw-r--r-- 1 tclee tclee 1071 May 24 19:44 LICENSE
5. tclee@raspberrypi:~/may24wed23/rpi-source $ file rpi-source
/usr/local/bin/rpi-source: Python script, ASCII text executable
6. tclee@raspberrypi:~/may24wed23/rpi-source $ dpkg --list git bc bison flex libssl-dev
.....
7. tclee@raspberrypi:~/may24wed23/rpi-source $ ./rpi-source
.....
8. tclee@raspberrypi:~/may24wed23/rpi-source $ dpkg --list libncurses5-dev
dpkg-query: no packages found matching libncurses5-dev
tclee@raspberrypi:~/may24wed23/rpi-source $ sudo apt install libncurses5-dev
.....
tclee@raspberrypi:~/may24wed23 $ cd rpi-source/
tclee@raspberrypi:~/may24wed23/rpi-source $ egrep -n build README.md
.....
tclee@raspberrypi:~/linux $ ls -l /lib/modules/6.1.28-v7+/build
lrwxrwxrwx 1 root root 17 May 24 20:56 /lib/modules/6.1.28-v7+/build -> /home/tclee/linux
tclee@raspberrypi:~/linux $
1. tclee@raspberrypi:~/may24wed23 $ git clone https://github.com/RPi-Distro/rpi-source.git
......
2. tclee@raspberrypi:~/may24wed23 $ ls -lrtag
.....
4 drwxr-xr-x 4 tclee 4096 May 24 19:44 rpi-source
3. tclee@raspberrypi:~/may24wed23 $ cd rpi-source/
4. tclee@raspberrypi:~/may24wed23/rpi-source $ ls -lrt
total 36
-rwxr-xr-x 1 tclee tclee 14223 May 24 19:44 rpi-source
drwxr-xr-x 3 tclee tclee 4096 May 24 19:44 debian
-rw-r--r-- 1 tclee tclee 11828 May 24 19:44 README.md
-rw-r--r-- 1 tclee tclee 1071 May 24 19:44 LICENSE
5. tclee@raspberrypi:~/may24wed23/rpi-source $ file rpi-source
/usr/local/bin/rpi-source: Python script, ASCII text executable
6. tclee@raspberrypi:~/may24wed23/rpi-source $ dpkg --list git bc bison flex libssl-dev
.....
7. tclee@raspberrypi:~/may24wed23/rpi-source $ ./rpi-source
.....
8. tclee@raspberrypi:~/may24wed23/rpi-source $ dpkg --list libncurses5-dev
dpkg-query: no packages found matching libncurses5-dev
tclee@raspberrypi:~/may24wed23/rpi-source $ sudo apt install libncurses5-dev
.....
tclee@raspberrypi:~/may24wed23 $ cd rpi-source/
tclee@raspberrypi:~/may24wed23/rpi-source $ egrep -n build README.md
.....
tclee@raspberrypi:~/linux $ ls -l /lib/modules/6.1.28-v7+/build
lrwxrwxrwx 1 root root 17 May 24 20:56 /lib/modules/6.1.28-v7+/build -> /home/tclee/linux
tclee@raspberrypi:~/linux $
Re: Cannot find build folder in /lib/module/$(name -r)/build folder for compiling linux kernel modules
Eventually I made the kernel header and kernel source matched on my RpiOS32bit/Rpi3MB platform. Now I am moving onto the exercise of the kernel modules I am interested on this platform. Shown below is the method I adopted. I notice the "sudo apt install raspberrypi-kernel-headers" execution creates the build symbolic link build to /usr/src/linux-headers-6.1.21-v7+:
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $ ls -l /usr/src/
total 4
drwxr-xr-x 3 root root 4096 May 2 17:14 sense-hat
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $ sudo apt install raspberrypi-kernel-headers
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
libfuse2
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
raspberrypi-kernel-headers
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 30.0 MB of archives.
After this operation, 193 MB of additional disk space will be used.
Get:1 http://archive.raspberrypi.org/debian bullseye/main armhf raspberrypi-kernel-headers armhf 1:1.20230405-1 [30.0 MB]
Fetched 30.0 MB in 3min 4s (163 kB/s)
apt-listchanges: Can't set locale; make sure $LC_* and $LANG are correct!
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_TIME = "zh_CN.UTF-8",
LC_MONETARY = "zh_CN.UTF-8",
LC_ADDRESS = "zh_CN.UTF-8",
LC_TELEPHONE = "zh_CN.UTF-8",
LC_NAME = "zh_CN.UTF-8",
LC_MEASUREMENT = "zh_CN.UTF-8",
LC_IDENTIFICATION = "zh_CN.UTF-8",
LANG = "en_GB.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
locale: Cannot set LC_ALL to default locale: No such file or directory
Selecting previously unselected package raspberrypi-kernel-headers.
(Reading database ... 106933 files and directories currently installed.)
Preparing to unpack .../raspberrypi-kernel-headers_1%3a1.20230405-1_armhf.deb ...
Unpacking raspberrypi-kernel-headers (1:1.20230405-1) ...
Setting up raspberrypi-kernel-headers (1:1.20230405-1) ...
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $ ls -l /usr/src/
total 16
drwxr-xr-x 25 root root 4096 Jun 7 11:56 linux-headers-6.1.21+
drwxr-xr-x 25 root root 4096 Jun 7 11:56 linux-headers-6.1.21-v7+
drwxr-xr-x 25 root root 4096 Jun 7 11:56 linux-headers-6.1.21-v7l+
drwxr-xr-x 3 root root 4096 May 2 17:14 sense-hat
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $ ls /lib/modules/$uname -r)
-bash: syntax error near unexpected token `)'
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $ ls /lib/modules/$(uname -r)
build modules.alias modules.builtin modules.builtin.bin modules.dep modules.devname modules.softdep modules.symbols.bin
odules.dep.bin modules.order modules.symbols
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $ ls -l /usr/src/
total 4
drwxr-xr-x 3 root root 4096 May 2 17:14 sense-hat
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $ sudo apt install raspberrypi-kernel-headers
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
libfuse2
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
raspberrypi-kernel-headers
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 30.0 MB of archives.
After this operation, 193 MB of additional disk space will be used.
Get:1 http://archive.raspberrypi.org/debian bullseye/main armhf raspberrypi-kernel-headers armhf 1:1.20230405-1 [30.0 MB]
Fetched 30.0 MB in 3min 4s (163 kB/s)
apt-listchanges: Can't set locale; make sure $LC_* and $LANG are correct!
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_TIME = "zh_CN.UTF-8",
LC_MONETARY = "zh_CN.UTF-8",
LC_ADDRESS = "zh_CN.UTF-8",
LC_TELEPHONE = "zh_CN.UTF-8",
LC_NAME = "zh_CN.UTF-8",
LC_MEASUREMENT = "zh_CN.UTF-8",
LC_IDENTIFICATION = "zh_CN.UTF-8",
LANG = "en_GB.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
locale: Cannot set LC_ALL to default locale: No such file or directory
Selecting previously unselected package raspberrypi-kernel-headers.
(Reading database ... 106933 files and directories currently installed.)
Preparing to unpack .../raspberrypi-kernel-headers_1%3a1.20230405-1_armhf.deb ...
Unpacking raspberrypi-kernel-headers (1:1.20230405-1) ...
Setting up raspberrypi-kernel-headers (1:1.20230405-1) ...
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $ ls -l /usr/src/
total 16
drwxr-xr-x 25 root root 4096 Jun 7 11:56 linux-headers-6.1.21+
drwxr-xr-x 25 root root 4096 Jun 7 11:56 linux-headers-6.1.21-v7+
drwxr-xr-x 25 root root 4096 Jun 7 11:56 linux-headers-6.1.21-v7l+
drwxr-xr-x 3 root root 4096 May 2 17:14 sense-hat
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $ ls /lib/modules/$uname -r)
-bash: syntax error near unexpected token `)'
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $ ls /lib/modules/$(uname -r)
build modules.alias modules.builtin modules.builtin.bin modules.dep modules.devname modules.softdep modules.symbols.bin
odules.dep.bin modules.order modules.symbols
tclee@raspberrypi:~/jun07wed23/rpilkm/RPI-LKM $