Chinaunix首页 | 论坛 | 博客
  • 博客访问: 82618
  • 博文数量: 18
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 321
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-30 21:09
文章分类

全部博文(18)

文章存档

2015年(3)

2014年(9)

2013年(6)

我的朋友

分类: LINUX

2014-07-24 22:38:35

下面内容参考于:
http://www.linuxdevcenter.com/pub/a/linux/2007/07/05/devhelloworld-a-simple-introduction-to-device-drivers-under-linux.html?page=1
http://blog.csdn.net/haoel/article/details/4134807

Makefile的组成:
# obj-m is a list of what kernel modules to build.  The .o and other
# objects will be automatically built from the corresponding .c file -
# no need to list the source files explicitly.
obj-m := hello_printk.o 
# KDIR is the location of the kernel source.  The current standard is
# to link to the associated source tree from the directory containing
# the compiled modules.
KDIR  := /lib/modules/$(shell uname -r)/build
# PWD is the current working directory and the location of our module
# source files.
PWD   := $(shell pwd)
# default is the default make target.  The rule here says to run make
# with a working directory of the directory containing the kernel
# source and compile only the modules in the PWD (local) directory.
default:
    $(MAKE) -C $(KDIR) M=$(PWD) modules

关于这个Makefile的含义,可以参考linux驱动程序编程这本书。
其中/lib/modules/$(shell uname -r)为当前内核的外部模块的保存路径,其中的build是个软链接,指向了当前核心的源代码。在编译驱动程序的时候,一定需要当前运行核心的源代码及其编译环境,需要源码的原因是因为驱动程序需要核心的include文件,需要核心的编译环境是驱动需要知道当前核心在编译时,选择了哪些模块及哪些高级选项

/*
 * "Hello, world!" minimal kernel module
 *
 * Valerie Henson
 *
 */


/*
 * The below are header files provided by the kernel which are
 * required for all modules.  They include things like the definition
 * of the module_init() macro.
 */
#include
#include


/*
 * This is the init function, which is run when the module is first
 * loaded.  The __init keyword tells the kernel that this code will
 * only be run once, when the module is loaded.
 */
static int __init
hello_init(void)
{
    printk("Hello, world!\n");
    return 0;
}


/*
 * The below macro informs the kernel as to which function to use as
 * the init function.
 */


module_init(hello_init);


/*
 * Similary, the exit function is run once, upon module unloading, and
 * the module_exit() macro identifies which function is the exit
 * function.
 */


static void __exit
hello_exit(void)
{
    printk("Goodbye, world!\n");
}


module_exit(hello_exit);


/*
 * MODULE_LICENSE() informs the kernel what license the module source
 * code is under, which affects which symbols it may access in the
 * main kernel.  Certain module licenses will "taint" the kernel,
 * indicating that non-open or untrusted code has been loaded.
 * Modules licensed under GPLv2 do not taint the kernel and can access
 * all symbols, but declaring it so is a legal statement that the
 * source code to this module is licensed under GPLv2, and so you must
 * provide the source code if you ship a binary version of the module.
 */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Valerie Henson ");
MODULE_DESCRIPTION("\"Hello, world!\" minimal module");
MODULE_VERSION("printk");

在当前目录下执行make命令即可
sudo insmod ./hello_printk.ko
$ dmesg | tail
sudo rmmod hello_printk
$ dmesg | tail



阅读(756) | 评论(0) | 转发(0) |
0

上一篇:内核网络驱动程序分析

下一篇:linux softirq

给主人留下些什么吧!~~