1. 編譯多個.c源文件的ko(單個文件編寫網上有太多的教程,多個c源文件編譯是這我這回遇到的)
2. 編寫帶參數的內核模塊(這個順便也給記下,雖然貌似沒有用到)
3. insmod,rmmod,mknod,modprobe,dmesg等命令使用(dmesg比較有用,有些人不知道用這個命令,就說要用串口顯示信息,其實dmesg命令更方便,-c選項清空信息)
4. 無法插入內核的原因。(這個也是關鍵,很多時候操作起來比較簡單,比如編譯內核源碼,但可能會碰到壹系列錯誤,解決壹堆錯誤最能提升壹個人的能力)
壹、編譯多個.c文件的ko模塊並插入內核
//test_module.c
#include <linux/init.h>
#include <linux/module.h>
#include "extern_app.h"
static char *who = "linux ss";
static int many = 1;
module_param(many, int, S_IRUGO);
module_param(who, charp, S_IRUGO);
static int __init hello_init(void)
{
test();
static int i = 0;
for(i = 0; i < many; i++)
printk(KERN_ALERT "Hello, %s!\n", who);
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_ALERT "Goodbye\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
//extern_app.c
#include <linux/init.h>
#include <linux/module.h>
int test(void)
{
printk("test here.\n");
return 0;
}
//extern_app.h
#ifndef __EXTERN_APP_H
#define __EXTERN_APP_H
extern int test(void);
#endif
//Makefile
#ifneq ($(KERNELRELEASE),)
obj-m := my_module.o
my_module-objs := test_module.o extern_app.o
#else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)