在centos 6上的helloworld 模块测试


[root@localhost test]# ls
helloworld.c  Makefile
[root@localhost test]# make
make -C /lib/modules/2.6.32-71.29.1.el6.i686/build M=/home/android/test modules
make[1]: Entering directory `/usr/src/kernels/2.6.32-71.29.1.el6.i686'
  CC [M]  /home/android/test/helloworld.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/android/test/helloworld.mod.o
  LD [M]  /home/android/test/helloworld.ko.unsigned
  NO SIGN [M] /home/android/test/helloworld.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.32-71.29.1.el6.i686'
[root@localhost test]# ls
helloworld.c            helloworld.mod.c  Makefile
helloworld.ko           helloworld.mod.o  modules.order
helloworld.ko.unsigned  helloworld.o      Module.symvers
[root@localhost test]# insmod helloworld.ko
insmod: error inserting 'helloworld.ko': -1 File exists
[root@localhost test]# rmmod helloworld.ko
[root@localhost test]# ls
helloworld.c            helloworld.mod.c  Makefile
helloworld.ko           helloworld.mod.o  modules.order
helloworld.ko.unsigned  helloworld.o      Module.symvers
[root@localhost test]# insmod helloworld.ko
[root@localhost test]# modinfo helloworld.ko
filename:       helloworld.ko
author:         yuanwei
license:        GPL
srcversion:     E074EFF1EF4BF929639B50D
depends:
vermagic:       2.6.32-71.29.1.el6.i686 SMP mod_unload modversions 686
[root@localhost test]#

附上 helloworld 源码和Makefile

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

static int __init hellokernel_init(void)
{
        printk(KERN_INFO "Hello kernel!\n");
        return 0;
}

static void __exit hellokernel_exit(void)
{
        printk(KERN_INFO "Exit kernel!\n");
}


module_init(hellokernel_init);
module_exit(hellokernel_exit);


MODULE_LICENSE("GPL");
MODULE_AUTHOR("yuanwei");
~
~
~
~

obj-m := helloworld.o

PWD       := $(shell pwd)

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
        rm -rf *.o *~ core .*.cmd *.mod.c ./tmp_version *.ko.* modules.* Module.*


如果是CentOS 平台。需要安装kernel头文件

需要用YUM安装

[root@localhost test]# yum install kernel-devel
Loaded plugins: fastestmirror, refresh-packagekit
Loading mirror speeds from cached hostfile
 * base: data.nicehosting.co.kr
 * extras: data.nicehosting.co.kr
 * updates: data.nicehosting.co.kr
Setting up Install Process
Package kernel-devel-2.6.32-71.29.1.el6.i686 already installed and latest version
Nothing to do



原文地址:https://www.cnblogs.com/yuzaipiaofei/p/4124408.html