您尚未登录。

楼主 #1 2019-06-14 22:42:20

firstman
会员
注册时间: 2019-04-06
已发帖子: 279
积分: 279

Ubuntu 新建一个 hello 驱动程序

参考网上例程


这是hello.c:

hello.c 说:

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

static int hello_init(void)
{
    printk(KERN_INFO "Hello world enter!\n");
}

static void hello_exit(void)
{
    printk(KERN_INFO "Hello world exit!\n");
}

module_init(hello_init)
module_exit(hello_exit)

MODULE_AUTHOR("hexing");
MODULE_LICENSE("dual BSD/GPL");
MODULE_DESCRIPTION("SIMPLE");
MODULE_ALIAS("simple");

这是Makefile:

KVERS =$(shell uname -r)

obj-m += hello.o

build: kernel_modules
 
kernel_modules:
	make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules

clean:
	make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean

直接 make 即可

最近编辑记录 firstman (2019-06-14 22:42:37)

离线

楼主 #2 2019-06-14 22:45:22

firstman
会员
注册时间: 2019-04-06
已发帖子: 279
积分: 279

Re: Ubuntu 新建一个 hello 驱动程序

firstman@ubuntu:/opt/test$ make
make -C /lib/modules/4.10.0-38-generic/build M=/opt/test modules
make[1]: Entering directory '/usr/src/linux-headers-4.10.0-38-generic'
  CC [M]  /opt/test/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /opt/test/hello.mod.o
  LD [M]  /opt/test/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.10.0-38-generic'

firstman@ubuntu:/opt/test$ sudo insmod hello.ko 
firstman@ubuntu:/opt/test$ 

firstman@ubuntu:/opt/test$ sudo lsmod |grep hello
hello                  16384  0

离线

#3 2020-06-06 09:11:41

哦豁哦豁
会员
注册时间: 2020-01-17
已发帖子: 79
积分: 79

Re: Ubuntu 新建一个 hello 驱动程序

可以用, 谢谢楼主,一开始以为编译内核模块要下载整个kernel源码,直到找到楼主的这个代码才知道并不需要。

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn