不得不说网上坑爹的文章比虱子还多,参考这位仁兄调试成功
喜欢C的人却靠着Java产业吃饭,人艰不拆...
对于未知的东西,有个习惯,run success first,then research
environment
[root@vohst etc]# uname -a
Linux vohst 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@vohst bin]# make
make -C /lib/modules/3.10.0-123.el7.x86_64/build/ M=/home/voh/bin modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-123.el7.x86_64'
CC [M] /home/voh/bin/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/voh/bin/hello.mod.o
LD [M] /home/voh/bin/hello.ko
make[1]: Leaving directory `/usr/src/kernels/3.10.0-123.el7.x86_64'
[root@vohst bin]#
command(root):
make
insmod
lsmod | head
rmmod
[17842.192368] Hello World!
[18340.765263] Good bye, ubuntu
[18413.168837] Hello World!
[18832.134751] Good bye, ubuntu //i like centos and suse,but suse is allways not purl
不会打印到屏幕,不重要,logdir=/var/log/dmesg,不是每次都能找到呢...
c & Makefile
//Begin---hello.c
//my kernel path
//[root@vohst bin]# ls /usr/src/kernels/3.10.0-123.el7.x86_64/include/linux/module.h
#include</usr/src/kernels/3.10.-.el7.x86_64/include/linux/init.h>
#include</usr/src/kernels/3.10.-.el7.x86_64/include/linux/module.h> MODULE_LICENSE("GPL");
//printk(KERN_ALERT "Begin\n");
static int hello_init(void)
{
printk(KERN_ALERT "Hello World!\n");
return ;
} static void hello_exit(void)
{
printk(KERN_ALERT "Good bye, ubuntu\n");
// return 0;
} module_init(hello_init);
module_exit(hello_exit);
//End---hello.c
#Begin---Makefile
KERNELDIR=/lib/modules/3.10.-.el7.x86_64/build/
PWD:=$(shell pwd)
INSTALLDIR=/home/voh/bin
obj-m:= hello.o
modules:
#this line should begins with "#"
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
##########
cp hello.ko $(INSTALLDIR)
clean:
##########
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
#End---Makefile
---------------------------------------------------------------------------------------------
http://blog.csdn.net/fareast8612/article/details/7470386
【0】笔者的配置环境
XP->VMWare 7.1->Ubuntu 9.04
【1】有必要查询下Linux内核
# uname -r
2.6.28-11-generic
# ls /usr/src/
linux-headers-2.6.28-11 linux-headers-2.6.28-11-generic
由此可见内核版本和内核头文件版本是一致的,都是2.6.28-11。(如果不一致的话在insmod一步必定出错:
Error inserting './hello.ko': -1 Invalid module format
网上有纠正这个错误的方法,但是感觉是在投机——躲避内核的版本检查;笔者在安装Ubuntu 8.04的时候出现过header头文件和内核版本不匹配的问题,后来通过重装Ubuntu为9.04解决之)。
【2】编写hello.c
新建自己的工作目录,如:
# mkdir /home/wk/hello
编写hello.c
# cd /home/wk/hello
# gedit hello.c
加入以下内容:
//Begin---hello.c
#include</usr/src/linux-headers-2.6.28-11/include/linux/init.h>
#include</usr/src/linux-headers-2.6.28-11/include/linux/module.h>
MODULE_LICENSE("GPL");
//printk(KERN_ALERT "Begin\n");
static int hello_init(void)
{
printk(KERN_ALERT "Hello World!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Good bye, ubuntu\n");
// return 0;
}
module_init(hello_init);
module_exit(hello_exit);
//End---hello.c
注意第一行
#include</usr/src/linux-headers-2.6.28-11/include/linux/init.h>
位置要正确;或者你只要写成
#include<linux/init.h>
保存退出(Ctrl+Q)。
【3】编写Makefile
# cd /home/wk/hello
# gedit Makefile
注意大小写。
#Begin---Makefile
KERNELDIR=/lib/modules/2.6.28-11-generic/build
PWD:=$(shell pwd)
INSTALLDIR=/home/wk/hello/install
obj-m:= hello.o
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
cp hello.ko $(INSTALLDIR)
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
#End---Makefile