Linux 如何为树莓派编译内核模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20167411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile a kernel module for Raspberry pi?
提问by user3025582
I'm having trouble compiling a kernel module for a raspberry pi. I want to compile a "hello world" kernel module using the raspberry pi itself.
我在为树莓派编译内核模块时遇到问题。我想使用树莓派本身编译一个“hello world”内核模块。
I am using raspbian wheezy 3.6.11+.
我正在使用 raspbian wheezy 3.6.11+。
I tried following the directions at http://elinux.org/RPi_Kernel_Compilation.
我尝试按照http://elinux.org/RPi_Kernel_Compilation 上的说明进行操作。
Here is the Makefile I am using:
这是我正在使用的 Makefile:
obj-m += hello-1.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Here is the source code for hello-1.c:
这是 hello-1.c 的源代码:
/*
* hello-1.c - The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
Here's what I get when I try to make the project:
这是我尝试制作项目时得到的结果:
root@raspberrypi:/home/pi/hello-module# make
make -C /lib/modules/3.6.11+/build M=/home/pi/hello-module modules
make: *** /lib/modules/3.6.11+/build: No such file or directory. Stop.
make: *** [all] Error 2
I tried creating the build directory at /lib/modules/3.6.11+
我尝试在 /lib/modules/3.6.11+ 创建构建目录
make -C /lib/modules/3.6.11+/build M=/home/pi/hello-module modules
make[1]: Entering directory `/lib/modules/3.6.11+/build'
make[1]: *** No rule to make target `modules'. Stop.
make[1]: Leaving directory `/lib/modules/3.6.11+/build'
make: *** [all] Error 2
I have GNU Make 3.81 and gcc (Debian 4.6.3-14+rpi1) 4.6.3 installed. I also installed the linux source using
我安装了 GNU Make 3.81 和 gcc (Debian 4.6.3-14+rpi1) 4.6.3。我还使用安装了 linux 源
sudo apt-get install linux-source
sudo apt-get install linux-source
Any ideas on what I might do to get this to compile?
关于我可以做些什么来编译它的任何想法?
回答by cstick
I was working on the exact same sample on my RPI with the exact same kernel. I managed to compile the module on my RPI but when I issued insmod I received an error. I followed the instructions hereon an XUbuntu virtual machine (using my RPI's kernel version 3.6.y) and it worked perfectly. Not sure why compiling directly on the RPI did not work, that will be a problem for another day.
我正在使用完全相同的内核在 RPI 上处理完全相同的示例。我设法在我的 RPI 上编译了模块,但是当我发出 insmod 时我收到一个错误。我在 XUbuntu 虚拟机(使用我的 RPI 内核版本 3.6.y)上按照此处的说明进行操作,它运行良好。不知道为什么直接在 RPI 上编译不起作用,这将是另一天的问题。
I had to change the Makefile to match the new environment.
我不得不更改 Makefile 以匹配新环境。
obj-m += hello-1.o
all:
make ARCH=arm CROSS_COMPILE=${CCPREFIX} -C /home/cstick/rpi/linux-rpi-3.6.y M=$(PWD) modules
clean:
make -C /home/cstick/rpi/linux-rpi-3.6.y M=$(PWD) clean
回答by Greg
When compiling a module the -C
parameter should point to the source tree where the kernel was built (don't clean it up!). If you built it on the pi its likely in a directory under your home directory.
编译模块时,-C
参数应该指向构建内核的源代码树(不要清理它!)。如果您在 pi 上构建它,则它可能位于您的主目录下的目录中。
The build
directory under /lib/modules/<version>
is a Debian-ism, where a cut-down version of the source tree is provided with just enough context to build modules against. The kernels from the Raspberry Pi Foundation kernels don't ship with a build
directory.
下面的build
目录/lib/modules/<version>
是 Debian-ism,其中提供了源代码树的缩减版本,其中提供了足够的上下文来构建模块。来自 Raspberry Pi Foundation 内核的内核不附带build
目录。
They may be a bit out of date, but raspbian provides a kernel as a Debian-style package, which should include the build
directory you could use to build kernel modules against.
它们可能有点过时了,但是 raspbian 提供了一个内核作为 Debian 风格的包,其中应该包含build
您可以用来构建内核模块的目录。
sudo aptitude install linux-image-rpi-rpfv linux-headers-rpi-rpfv
回答by JayDee
You first need kernel headers (and the corresponding kernel binary) to build your module.
Like Greg said, the raspbian distribution provides the packages :
您首先需要内核头文件(以及相应的内核二进制文件)来构建您的模块。
就像 Greg 所说,raspbian 发行版提供了以下软件包:
sudo apt-get install linux-image-rpi-rpfv linux-headers-rpi-rpfv
Then, tell raspbian to boot your newly installed kernel (3.10-3-rpi for me).
Append this at end of /boot/config.txt
and reboot your Pi :
然后,告诉 raspbian 启动您新安装的内核(对我来说是 3.10-3-rpi)。
将其添加到末尾/boot/config.txt
并重新启动您的 Pi:
# Parameters to boot on raspbian kernel (linux-image-rpi-rpfv package)
kernel=vmlinuz-3.10-3-rpi
initramfs initrd.img-3.10-3-rpi followkernel
Then, modify your Makefile to point the freshly installed kernel headers :
然后,修改你的 Makefile 以指向新安装的内核头文件:
KERNEL_HEADERS=/lib/modules/$(shell uname -r)/build
obj-m := hello-1.o
all:
@$(MAKE) -C $(KERNEL_HEADERS) M=$(PWD) modules
clean:
@$(MAKE) -C $(KERNEL_HEADERS) M=$(PWD) clean
回答by HeatfanJohn
Here are the steps I used to build the Hello World kernel moduleon Raspbian.
以下是我用来在 Raspbian 上构建Hello World 内核模块的步骤。
Perform
sudo rpi-update
See https://github.com/Hexxeh/rpi-updatefor details on
rpi-update
. You have to be on the latest firmware and associated kernel to be able to perform the next step.Install and run
rpi-source
to install the source code that built the latest kernel that you are running. This will create the correct entry in/lib/modules
for the kernel that you are running. Note: you don't need to be root to run this, however the script will perform certain tasks usingsudo
and the root password will be requested during the script execution.Instructions to install
rpi-source
can be found at https://github.com/notro/rpi-source/wiki
履行
sudo rpi-update
有关的详细信息, 请参阅https://github.com/Hexxeh/rpi-update
rpi-update
。您必须使用最新的固件和相关内核才能执行下一步。安装并运行
rpi-source
以安装构建您正在运行的最新内核的源代码。这将为/lib/modules
您正在运行的内核创建正确的条目。注意:您不需要是 root 来运行它,但是脚本将使用执行某些任务sudo
并且在脚本执行期间将要求 root 密码。安装说明
rpi-source
可以在https://github.com/notro/rpi-source/wiki找到
Once those steps are performed you should be able to make
the Hello World kernel module.
执行这些步骤后,您应该能够访问make
Hello World 内核模块。
johnma@raspberrypi ~/HelloWorld $ make
make -C /lib/modules/3.12.19+/build M=/home/johnma/HelloWorld modules
make[1]: Entering directory `/home/johnma/linux-c3db7205bcd8988cf7c185e50c8849542554b1f5'
CC [M] /home/johnma/HelloWorld/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/johnma/HelloWorld/hello.mod.o
LD [M] /home/johnma/HelloWorld/hello.ko
make[1]: Leaving directory `/home/johnma/linux-c3db7205bcd8988cf7c185e50c8849542554b1f5'
johnma@raspberrypi ~/HelloWorld $ sudo insmod hello.ko
johnma@raspberrypi ~/HelloWorld $ tail -1 /var/log/syslog
May 15 13:45:39 raspberrypi kernel: [59789.169461] Hello World :)
johnma@raspberrypi ~/HelloWorld $ sudo rmmod hello.ko
johnma@raspberrypi ~/HelloWorld $ tail -1 /var/log/syslog
May 15 13:46:10 raspberrypi kernel: [59819.503503] Goodbye World!
回答by Ryu_hayabusa
This was a pain. I had to compile and install a kernel mode driver.After long search, i got the headers for pi 2 (3.18.7-v7+) from here.
这是一种痛苦。我必须编译并安装内核模式驱动程序。经过长时间的搜索,我从这里获得了 pi 2 (3.18.7-v7+) 的头文件。
sudo apt-get install dkms build-essential
wget http://www.niksula.hut.fi/~mhiienka/Rpi/linux-headers-rpi/linux-headers-3.18.7-v7%2b_3.18.7-v7%2b-2_armhf.deb
sudo dpkg -i linux-headers-3.18.7-v7+_3.18.7-v7+-2_armhf.deb
回答by Michael Hou
I meet with the same problem and just fix it by sudo apt-get install raspberrypi-kernel-headers
我遇到了同样的问题,只是通过 sudo apt-get install raspberrypi-kernel-headers