将多个源文件编译成一个ko

本文介绍如何将三个源文件(proc_rw.c、test.c和test.h)编译成一个名为proc.ko的内核模块。内容涉及到源文件间的函数调用,如proc_rw.c调用test.c的test_func函数。

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

有三个文件,分别是proc_rw.c、test.c和test.h,现在把这三个文件编译成proc.ko

1、proc_rw.c

#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/uaccess.h>  //copy_to|from_user

#include "test.h"
//在str中存储用户态write到文件的字符串
static char *str;

//proc文件的读函数
static int my_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
{
	int ret = 0,val = 0;
	
	//先输出时间,后输出从外部写入的内容
	//ret = sprintf(page, "kernel time: %ld\n", jiffies);
	val = test_func();
	ret = sprintf(page, "test func: %d\n", val);
	ret += sprintf(page+ret, "str is %s\n", str);

	return ret;
}

//proc文件的写函数
static int my_proc_write(struct file *filp, const char __user *buf, unsigned long count, void *data)
{
	//为从用户态拷贝字符串分配空间
	char *tmp = kzalloc(count, GFP_KERNEL);
	if (!tmp)
		return -ENOMEM;

	//将字符串从用户态拷贝到内核态
	if (copy_from_user(tmp, buf, c
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值