Openwrt添加內核模塊


轉載有道,先給出原文路徑:http://blog.csdn.net/l0605020112/article/details/13168261


下面就是增加內核模塊的方法了

進入package目錄,創建模塊目錄
cd backfire/package
mkdir example
進入example目錄,創建Makefile文件和代碼路徑
cd example
touch Makefile
mkdir src
 Makefile具體內容如下:

  1. #  
  2. # Copyright (C) 2008 OpenWrt.org  
  3. #  
  4. # This is free software, licensed under the GNU General Public License v2.  
  5. # See /LICENSE for more information.  
  6. #  
  7.   
  8. include $(TOPDIR)/rules.mk  
  9. include $(INCLUDE_DIR)/kernel.mk  
  10.   
  11. PKG_NAME:=example  
  12. PKG_RELEASE:=1  
  13.   
  14. include $(INCLUDE_DIR)/package.mk  
  15.   
  16. define KernelPackage/example  
  17.   SUBMENU:=Other modules  
  18.   TITLE:=example driver  
  19.   DEPENDS:=@LINUX_2_6  
  20.   FILES:=$(PKG_BUILD_DIR)/*.$(LINUX_KMOD_SUFFIX)  
  21.   KCONFIG:=  
  22. endef  
  23.   
  24. define KernelPackage/example/description  
  25.   Kernel module to example  
  26. endef  
  27.   
  28. EXTRA_KCONFIG:= \  
  29.     CONFIG_EXAMPLE=m  
  30.   
  31. EXTRA_CFLAGS:= \  
  32.     $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG)))) \  
  33.     $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) \  
  34.   
  35. MAKE_OPTS:= \  
  36.     ARCH="$(LINUX_KARCH)" \  
  37.     CROSS_COMPILE="$(TARGET_CROSS)" \  
  38.     SUBDIRS="$(PKG_BUILD_DIR)" \  
  39.     EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \  
  40.     $(EXTRA_KCONFIG)  
  41.   
  42. define Build/Prepare  
  43.     mkdir -p $(PKG_BUILD_DIR)  
  44.     $(CP) ./src/* $(PKG_BUILD_DIR)/  
  45. endef  
  46.   
  47. define Build/Compile  
  48.     $(MAKE) -C "$(LINUX_DIR)" \  
  49.         $(MAKE_OPTS) \  
  50.         modules  
  51. endef  
  52.   
  53. $(eval $(call KernelPackage,example))  


3.進入src目錄,創建代碼路徑和相關源文件
cd src
touch example.c Kconfig Makefile
  example.c具體內容如下:
  1. #include <linux/init.h>  
  2. #include <linux/module.h>  
  3. #include <linux/kernel.h>  
  4. /* hello_init ---- 初始化函數,當模塊裝載時被調用,如果成功裝載返回0 否則返回非0值 */  
  5. static int __init hello_init(void)  
  6. {  
  7.     printk("I bear a charmed life.\n");  
  8.     return 0;  
  9. }  
  10. /* hello_exit ---- 退出函數,當模塊卸載時被調用 */  
  11. static void __exit hello_exit(void)  
  12. {  
  13.     printk("Out, out, brief candle\n");  
  14. }  
  15. module_init(hello_init);  
  16. module_exit(hello_exit);  
  17. MODULE_LICENSE("GPL");  
  18. MODULE_AUTHOR("zhangjiefeng");  
  Kconfig具體內容如下:
  1. config EXAMPLE  
  2.   tristate "Just a example"  
  3.   help  
  4.    This is a example, for debugging kernel model.  
  5.    If unsure, say N.  
  Makefile具體內如如下:
  1. obj-$(CONFIG_EXAMPLE)   += example.o  
  回到主路徑 backfire/,編譯選項配置保存並編譯
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 (‘+=’).

以後要多加註意。

    

發佈了26 篇原創文章 · 獲贊 39 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章