Linux Kernel的第一个程序helloworld!!!

编写和测试环境:

OS:Suse 11 SP3

内核:Linux linux-jcvv 3.0.76-0.11-default #1 SMP Fri Jun 14 08:21:43 UTC 2013 (ccab990) x86_64 x86_64 x86_64 GNU/Linux

1.用vi编写一个hello.c

#ifndef __KERNEL__

        #define __KERNEL__
#endif
#ifndef MODULE
        #define MODULE
#endif
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int hello_init(void)
{
        printk("HELLO,my's computer.yes! the hello model is working!!\n");
        return 0;
}
static void hello_exit(void)
{
         printk("HELLO,my is leaving the hello world module!!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("albcamus<my@126.com");

2.编写Makefile

KERNELDIR=/lib/modules/3.0.76-0.11-default/build
PWD:=$(shell pwd)
INSTALLDIR=$(PWD)
#CC=arm-linux-gcc    
CC=gcc
obj-m := hello.o
modules:     
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules   
clean:     
        rm -rf *.o *.ko *.mod.c *.markers *.order *.symvers     

 .PHONY:modules clean

3.make执行,生成hello.o和hello.ko文件

在编译的时候遇到了如下的问题:

如果报下面的错误:
1)错误提示:
/bin/sh: scripts/mod/modpost: No such file or directory
出现这样的错误,说明scripts下没有生成相应的文件,cd到/usr/src/linux/kernel所在目录,执行: make scripts
搞定,

然后 make ,就没有这样的错误了.

2)ERROR: Kernel configuration is invalid.

         include/generated/autoconf.h or include/config/auto.conf are missing.

         Run 'make oldconfig && make prepare' on kernel src to fix it.

cd /usr/src/linux/

执行make oldconfig && make prepare 

3)如果编译成功后,加载模块失败报:这个报错的原因是Makefile中的内核目录是/usr/src/linux导致。
insmod: error inserting 'hello.ko': -1 Invalid module format
修改Makefile的内核目录,改为:/lib/modules/3.0.76-0.11-default/build

4.编译成功后,跟踪系统日志文件/var/log/messages

#tail -f messages

Apr 25 04:02:30 linux-jcvv sshd[4505]: Accepted keyboard-interactive/pam for root from 192.168.72.1 port 50063 ssh2
Apr 25 04:03:09 linux-jcvv kernel: [ 4409.346489] hello: no symbol version for module_layout
Apr 25 04:08:32 linux-jcvv kernel: [ 4732.096586] HELLO,my's computer.yes! the hello model is working!!
Apr 25 04:10:07 linux-jcvv kernel: [ 4827.932378] HELLO,my is leaving the hello world module!!



                                               
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值