您的位置:首页 > 运维架构 > Linux

Linux内核编程 <1> -- Hello Mod

2016-05-27 16:31 501 查看

1. hello mod

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

static int __init hello_start(void){
printk("hello,this moudle start work!\n");
return 0;
}

static void __exit hello_exit(void){
printk("hello,this moule exit!\n");
}

module_init(hello_start);
module_exit(hello_exit);


2.Makefile

obj-m=hello_ker.o
CURRENT_PATH:=$(shell pwd)
LINUX_KERNEL:=$(shell uname -r)
LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)

all:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
clean:
mkae -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean


一些问题

1) 在Makefile中,其中LINUX_PATH :=$(shell uname -r)很重要,内核的程序在编译时,需要的头文件当然得符合当前系统的版本。当然也可以指定编译的版本,但前提是自己得有相应的linux源代码或者头文件。

2) 关于C语言的语法问题。由于自身C的编程习惯,在传参时如果函数参数没有参数,一般会以函数()结束

static int __exit printkhello()


但是在 make编译的过程中,会出现以下问题:

error: function declaration isn’t a prototype [-Werror=strict-prototypes]


意思大意就是函数原型不匹配的错误。

后来解决方法是在func()括号中添加了func(void)结束,在linux中的内核编程中,以后用void表示无参就可以了。

3) 接下来这个问题就比较坑爹了。由于英语水平不高,以至于module单词接连拼错,出现了很多的error。后来和朋友交流,起初他们也常犯这些低级错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux内核编程