module开发工程目录

本文探讨了Linux模块开发中的目录组织结构,讲解了如何处理不同模块间的调用,以及如何利用多个源文件生成一个KO文件。通过EXPORT_SYMBOL来导出函数,使得其他模块可以使用这些功能。同时,文章还提到了在同一模块中多次导出同一函数的尝试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、目录组织结构如下,本文主要讨论几个问题:不同模块之间如何相互调用;如何使用多个源文件生成一个ko文件;采用多个子目录

2、模块通过EXPORT_SYMBOL导出函数,供其他模块使用.hello_.c

#include <linux/init.h>
#include <linux/module.h>
#include "common.h"


static int hello_init(void){
	printk(KERN_ALERT "Hello ,modules world!\n");
	lib1();
	return 0;
}

static void hello_exit(void){
	printk(KERN_ALERT "Goodby,cruel world\n");
}

void func(void){
	printk("This is func\n");
}


MODULE_LICENSE("Dual BSD/GPL");
module_init(hello_init);
module_exit(hello_exit);


EXPORT_SYMBOL(func);
然后在hello2.c中就可以直接使用了:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

//This function is define in module hello
void func(void);

int __init hello_init(void){
	printk("hello 2 !\n");
	func();
	return 0;
}

void __exit hello_exit(void){
	printk("hello 2 exit\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("Dual BSD/GPL");
3、使用多个源文件生成一个ko文件,详细参照Kbuild文档:

obj-m := hello.o

#This will link hello_.o and common.o to hello.ko
hello-objs += hello_.o common.o

4、项目主(根)目录下的Makefile改为:

obj-m += hello/
obj-m += hello2/
obj-m += hello3/
make时将自动执行hello/目录下的Makefile文件


5、尝试在多处EXPORT_SYMBOL同一函数:在hello3.c中又定义一个名为func的函数,并导出:

echo "Kernel version: `uname -r`"
Kernel version: 3.13.0-24-generic
echo "kernel sources: "/lib/modules/`uname -r`/build""
kernel sources: /lib/modules/3.13.0-24-generic/build
make -C "/lib/modules/`uname -r`/build" SUBDIRS=`pwd` modules
make[1]: Entering directory `/usr/src/linux-headers-3.13.0-24-generic'
  CC [M]  /home/zhijian/work/fs/module_test/hello/hello_.o
  CC [M]  /home/zhijian/work/fs/module_test/hello/common.o
  LD [M]  /home/zhijian/work/fs/module_test/hello/hello.o
  CC [M]  /home/zhijian/work/fs/module_test/hello2/hello2.o
  CC [M]  /home/zhijian/work/fs/module_test/hello3/hello3.o
  Building modules, stage 2.
  MODPOST 3 modules
WARNING: /home/zhijian/work/fs/module_test/hello3/hello3: 'func' exported twice. Previous export was in /home/zhijian/work/fs/module_test/hello/hello.ko
  CC      /home/zhijian/work/fs/module_test/hello/hello.mod.o
  LD [M]  /home/zhijian/work/fs/module_test/hello/hello.ko
  CC      /home/zhijian/work/fs/module_test/hello2/hello2.mod.o
  LD [M]  /home/zhijian/work/fs/module_test/hello2/hello2.ko
  CC      /home/zhijian/work/fs/module_test/hello3/hello3.mod.o
  LD [M]  /home/zhijian/work/fs/module_test/hello3/hello3.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-24-generic'
得到警告 'func'exported twice。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值