轉載:源沒有記住
注意:example/Makefile中的PKG_BUILD_DIR沒有定義,不知道是否有影響,但是我添加了一句PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
注意:如果需要編譯多個檔案,可以在src下面的Makefile增加"example-y := example2.o",就可以增加一個example2.c
關于核心子產品的自動加載:
1 FILES:=$(PKG_BUILD_DIR)/fnsr-i2c$(PKG_MODULE_EXTENSION) 用于将制定的ko檔案拷貝到root-fs/lib/modules目錄下
2 AUTOLOAD:=$(call AutoLoad,99,fnsr-i2c) 應該是用于自動裝載子產品,在linux 2.6上工作正常,在linux 3.10不能生效,仍然需要手動輸入insmod
可以在Makefile增加下面代碼(在files下建立腳本my_module_start.sh)
define KernelPackage/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_BIN) files/my_module_start.sh $(1)/etc/init.d
endef
進入package目錄,建立子產品目錄
cd mcp/branches/V1.1-beta1/mcp/package
mkdir example
進入example目錄,建立Makefile檔案和代碼路徑
cd example
touch Makefile
mkdir src
Makefile具體内容如下:
# Kernel module example
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
DEPENDS:[email protected]_octeon
TITLE:=Support Module for example
AUTOLOAD:=$(call AutoLoad,81,example)
FILES:=$(PKG_BUILD_DIR)/example/example.$(LINUX_KMOD_SUFFIX)
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) -R ./src/* $(PKG_BUILD_DIR)/
endef
define Build/Compile
$(MAKE) -C "$(LINUX_DIR)" \
CROSS_COMPILE="$(TARGET_CROSS)" \
ARCH="$(LINUX_KARCH)" \
SUBDIRS="$(PKG_BUILD_DIR)/example" \
EXTRA_CFLAGS="-g $(BUILDFLAGS)" \
modules
endef
$(eval $(call KernelPackage,example))
3.進入src目錄,建立代碼路徑和相關源檔案
cd src
mkdir example
cd example
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("xxxx");
Kconfig具體内容如下:
config hello
tristate "Just a example"
default n
help
This is a example, for debugging kernel model.
If unsure, say N.
Makefile具體内如如下:
obj-m := example.o
回到主路徑 mcp/branches/V1.1-beta1/mcp/,編譯選項配置儲存并編譯
make menuconfig
Kernel modules --->
Other modules --->
kmod-example
選項設定為M,儲存退出
然後編譯該子產品:make package/example/compile