程式碼需要放置於下列目錄,及需要下列檔案
package/kernel/helloworld/
├── Makefile
└── src
├── helloworld.c
└── Makefile
第一層的 Makefile內容
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=helloworld
PKG_RELEASE:=1.0
include $(INCLUDE_DIR)/package.mk
define KernelPackage/helloworld
SUBMENU:=Other modules
TITLE:=helloworld
FILES:=$(PKG_BUILD_DIR)/helloworld.ko
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Build/Compile
$(MAKE) -C "$(LINUX_DIR)" V=1 \
CROSS_COMPILE="$(TARGET_CROSS)" \
ARCH="$(LINUX_KARCH)" \
SUBDIRS="$(PKG_BUILD_DIR)" \
modules
endef
$(eval $(call KernelPackage,helloworld))
helloworld.c 的範例程式
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
src/Makefile 內容為
obj-m += helloworld.o
make menuconfig 的選擇,將其選擇modules
Kernel modules --->
Other modules --->
< > kmod-helloworld...................................... helloworld
可以透過下列指令編譯
make package/kernel/helloworld/compile V=s
當安裝進入系統後,可以透過下列方式啟用模組
insmod helloworld.ko
lsmod
rmmod helloworld
參考資料