How to: Compile Linux kernel modulesby LinuxTitli · 36 comments This is one the essential and important task. Many time we upgrade our kernel and some precompiled drivers won't work with Linux. Especially if you have weird hardware; then vendor may send you driver code aka C files to compile. Or even you can write your own Linux kernel driver. Compiling kernel driver is easy. Kernel 2.6.xx makes it even much more easier. Following steps are required to compile driver as module: 1) You need running kernel source code; if you don't have a source code download it from . Untar kernel source code (tar ball) in /usr/src using tar command: To be frank kernel headers are more than sufficient to compile kernel modules / drivers. See how to install kernel headers under Debian / Ubuntu Linux or RHEL / CentOS / Fedora Linux. 2) Next go to your kernel module source code directory and simply
create the Makefile file as follows (assuming your kernel module name
is foo): 3) Add following text to it: obj-m = foo.o 4) Compile module using make command (module build can be done by any user) : 5) Once module compiled successfully, load it using insmod or
modprobe command. You need to be root user or privileged user to run
insmod: Example: hello.c module1) hello.c C source code. Copy following code and save to hello.c 2)Add following c source code to it: #include <linux/module.h> /* Needed by all modules */ This is an example modified from original source for demonstration purpose. 3) Save the file. Create new Makefile as follows: obj-m = hello.o 4) Save and close the file. 5) Compile hello.c module: 6) Become a root user (use su or sudo) and load the module: Note you can see message on screen if you are logged in as root under run level 3. 7) Verify that module loaded: 8) See message in /var/log/message file: 9) Unload the module: 10) Load module when Linux system comes up. File /etc/modules use to load kernel boot time. This file should contain the names of kernel modules that are to be loaded at boot time, one per line. First copy your module to /lib/modules/$(uname -r)/kernel/drivers. Following are suggested steps: (a) Create directory for hello module: See also:
|
|
来自: danydany_ok > 《makefile》