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

 

/* 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 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。