首页 > 系统服务 > 详细

A general "Hello World" src code for linux3.0 driver programing

时间:2015-01-02 10:56:48      阅读:363      评论:0      收藏:0      [点我收藏+]

 

/* hello.c */
#include <linux/kernel.h>
#include <linux/module.h>
static int hello_init(void)
{
    printk(KERN_ALERT "hello init"\n);
    return 0;
}
static void hello_exit(void)
{
    printk(KERN_ALERT "hello exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL");

 

A Makefile format before  linux2.6 (will encountered with error: include/linux/linkage.h:5:25: fatal error: asm/linkage.h: No such file or directory)

#Makefile
#Makefile for a basic kernel module
CC=gcc
MODCFLAGS:= -Wall -DMODULE -D__KERNEL__ -DLINUX
INCLUDE:= -IpathToLinuxKernelSrcInclude
hello.o:hello.c 
    $(CC) $(MODCFLAGS) $(INCLUDE) -c hello.c
    echo insmod hello.o to turn it on
    echo rmmod hello to turn if off
    echo
    echo X and kernel programming do not mix.
    echo Do the insmod and rmmod from outside X
# Note: A "tab" before "$(CC) xxx" means this is a shell command

A Makefile format as KBuild after linux2.6

# Makefile
obj-m := hello.o
# Use "make -C /opt/linux-3.0.4 SUBDIRS=$PWD modules" to compile this driver
# insmod hello.ko
# cat /proc/modules
# lsmod

 

For KBuild kernel driver programming, pls reference to The Linux Kernel Module Programming Guide 

A general "Hello World" src code for linux3.0 driver programing

原文:http://www.cnblogs.com/kozmers/p/4198177.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!