转载有道,先给出原文路径:http://blog.csdn.net/l0605020112/article/details/13168261
下面就是增加内核模块的方法了
进入package目录,创建模块目录
cd backfire/package
mkdir example
进入example目录,创建Makefile文件和代码路径
cd example
touch Makefile
mkdir src
Makefile具体内容如下:
- #
- # Copyright (C) 2008 OpenWrt.org
- #
- # This is free software, licensed under the GNU General Public License v2.
- # See /LICENSE for more information.
- #
- 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>
- /* hello_init ---- 初始化函数,当模块装载时被调用,如果成功装载返回0 否则返回非0值 */
- static int __init hello_init(void)
- {
- printk("I bear a charmed life.\n");
- return 0;
- }
- /* hello_exit ---- 退出函数,当模块卸载时被调用 */
- 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("zhangjiefeng");
- config EXAMPLE
- tristate "Just a example"
- help
- This is a example, for debugging kernel model.
- If unsure, say N.
- obj-$(CONFIG_EXAMPLE) += example.o
make menuconfig
Kernel modules --->
Other modules --->
kmod-example
选项设置为M,保存退出
然后编译该模块:
make package/example/compile
5.编译出的文件可以在主路径的以下路径找到
./staging_dir/target-mips_r2_uClibc-0.9.30.1/root-lantiq/lib/modules/2.6.32.33/example.ko
./build_dir/linux-lantiq_ar9/example/ipkg-lantiq/kmod-example/lib/modules/2.6.32.33/example.ko
./build_dir/linux-lantiq_ar9/example/example.ko
./build_dir/target-mips_r2_uClibc-0.9.30.1/OpenWrt-SDK-lantiq-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1/staging_dir/target-mips_r2_uClibc-0.9.30.1/root-lantiq/lib/modules/2.6.32.33/example.ko
./build_dir/target-mips_r2_uClibc-0.9.30.1/root-lantiq/lib/modules/2.6.32.33/example.ko
注:我们使用./build_dir/linux-lantiq_ar9/example/example.ko
对出现的错误和解决进行一些总结。
看似一个很简单的流程,做起来也是出现了一些问题。希望自己以后不要再犯错。
1,出现错误 *** No rule to make target ` '. Stop.
在makefile 行连接符“\” 后面多了空格。如果其他类似错误,都是由于复制文本的时候出现的空格,或者前面不是tab开头的
2 出现错误 *** Recursive variable `ARCH' references itself (eventually). Stop.
make官网是这么说的。
-
‘Recursive variable `xxx' references itself (eventually). Stop.’
-
This means you’ve defined a normal (recursive)
make
variablexxx that, when it’s expanded, will refer to itself (xxx).This is not allowed; either use simply-expanded variables (‘:=’or ‘::=’) or use the append operator (‘+=’).