这里的开发环境是在Ubuntu下,搭建好OpenWrt的交叉编译环境,这里不多说了,网上有很多教程。本次示例选择的OpenWrt版本是trunk版. 接下来是添加模块具体步骤: 1.进入trunk的package文件夹,创建模块目录: cd trunk/package mkdir example 2.进入example目录,创建Mackefile文件和代码路径: cd example touch Mackefile mkdir src Makefile代码如下:
include $(TOPDIR)/rules.mk include $(INCLUDE_DIR)/kernel.mk PKG_NAME:=example PKG_RELEASE:=1 include $(INCLUDE_DIR)/package.mk define KernelPackage/example SUBMENU:=Other modules TITLE:=example driver DEPENDS:=@LINUX_2_6 FILES:=$(PKG_BUILD_DIR)/*.$(LINUX_KMOD_SUFFIX) KCONFIG:= endef define KernelPackage/example/description Kernel module to example endef EXTRA_KCONFIG:= \ CONFIG_EXAMPLE=m EXTRA_CFLAGS:= \ $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG)))) \ $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) \ MAKE_OPTS:= \ ARCH="$(LINUX_KARCH) " \ CROSS_COMPILE="$(TARGET_CROSS) " \ SUBDIRS="$(PKG_BUILD_DIR) " \ EXTRA_CFLAGS="$(EXTRA_CFLAGS) " \ $(EXTRA_KCONFIG) define Build/Prepare mkdir -p $(PKG_BUILD_DIR) $(CP) ./src/* $(PKG_BUILD_DIR)/ endef define Build/Compile $(MAKE) -C "$(LINUX_DIR) " \ $(MAKE_OPTS) \ modules endef $(eval $(call KernelPackage,example))
3.进入src目录,创建代码路径和相关源文件 cd src touch example.c Kconfig Makefile example.c代码:
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> static int __init hello_init (void ) { printk("I bear a charmed life.\n" ); return 0 ; } static void __exit hello_exit (void ) { printk("Out, out, brief candle\n" ); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL" ); MODULE_AUTHOR("liuxie" );