驱动开发(第一个程序Hello World)

本文介绍了一个简单的Linux模块编写过程,包括创建目录、编写模块代码、设置Makefile等步骤,并解决了在构建过程中可能遇到的问题。

首先新建一个目录(比如):

mkdir helloworld

然后进入到这个目录:

cd helloworld

第一步:

vi hello.c

#include <linux/init.h>

#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)

{

        printk(KERN_ALERT "Hello ,world\n");

        return 0;

}

static void hello_exit(void)

{

        printk(KERN_ALERT "Goodbye,cruel world\n");

}

module_init(hello_init);

module_exit(hello_exit);

第二步:

vi Makefile

obj-m := hello.o

KDIR := /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

default:

        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

第三步:

基本的程序写好后,运行时遇到的一些问题和解决方案:

[lijun@myhost helloworld]$ /lib/modules/$(shell uname -r)/build

bash: shell: command not found

bash: /lib/modules//build: 没有那个文件或目录

[lijun@myhost helloworld]$ echo /lib/modules/$(uname -r)/build

/lib/modules/2.6.35-ARCH/build

[lijun@myhost helloworld]$ ls /lib/modules/$(uname -r)/build

ls: 无法访问/lib/modules/2.6.35-ARCH/build: 没有那个文件或目录

[lijun@myhost helloworld]$ pacman -Ss header

core/kernel26-headers 2.6.35.8-1

    Header files and scripts for building modules for kernel26

core/kernel26-lts-headers 2.6.32.25-2


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值