轉載有道,先給出原文路徑: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 (‘+=’).