目的:编译Linux module
系统:Linux 3.10.0-514.6.1.el7.x86_64, CentOS 7
源代码:
hello.c :
#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
static int year=2017;
MODULE_PARM(year,"i");
int hello_init()
{
printk(KERN_INFO"Hello World %d!\n",year);
return 0;
}
void hello_exit()
{
printk("Hello Exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile:
# The path of kernel source code
INCLUDEDIR = /usr/src/kernels/3.10.0-514.6.1.el7.x86_64/include/
# Compiler
CC = gcc
# Options
CFLAGS = -D__KERNEL__ -DMODULE -O -Wall -I$(INCLUDEDIR)
# Target
OBJS = hello.o
all: $(OBJS)
$(OBJS): hello.c
$(CC) $(CFLAGS) -c $<
install:
insmod $(OBJS)
uninstall:
rmmod hello
.PHONY: clean
clean:
rm -f *.o
系统:Linux 3.10.0-514.6.1.el7.x86_64, CentOS 7
源代码:
hello.c :
#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
static int year=2017;
MODULE_PARM(year,"i");
int hello_init()
{
printk(KERN_INFO"Hello World %d!\n",year);
return 0;
}
void hello_exit()
{
printk("Hello Exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile:
# The path of kernel source code
INCLUDEDIR = /usr/src/kernels/3.10.0-514.6.1.el7.x86_64/include/
# Compiler
CC = gcc
# Options
CFLAGS = -D__KERNEL__ -DMODULE -O -Wall -I$(INCLUDEDIR)
# Target
OBJS = hello.o
all: $(OBJS)
$(OBJS): hello.c
$(CC) $(CFLAGS) -c $<
install:
insmod $(OBJS)
uninstall:
rmmod hello
.PHONY: clean
clean:
rm -f *.o