码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2015-01-02 10:58:09      阅读:213      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!