【问题标题】:makefile in linux, unable to insert the modulelinux中的makefile,无法插入模块
【发布时间】:2016-06-07 03:06:13
【问题描述】:

我是 linux 编程的新手,我正在努力创建 makefile。我有3个文件: 你好.c

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include "funcs.h"

int init_module(void) {
    asgard();
    return 0;
 }

void cleanup_module(void) {
     printk(KERN_INFO "Goodbye world 1.\n");
}

funcs.c

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include "funcs.h"

void asgard(void) {
    printk(KERN_INFO "Asgard balordo\n");
    return;
}

funcs.h

#include <linux/module.h> /* Needed by all modules */

void asgard(void);

然后是makefile:

obj-m += hello.o
hello-objs := funcs.o

first:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

all: funcs.o hello.o
    gcc -o start funcs.o hello.o

funcs.o: funcs.c funcs.h
    gcc -C funcs.c

hello.o: hello.c funcs.h
    gcc -C hello.c

clean:
    rm -f *.o
    rm -f ./start
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

当我编译时,一切都很好,当我调用 insmod ./hello.ko 时,据说无法插入模块。 请你告诉我哪里错了吗?

【问题讨论】:

  • 有2个源文件,hello.c,和funcs.c,hello.o这里有歧义。所以请尝试将 hello.c 重命名为 hello_1.c,并设置 hello-objs := funcs.o hello_1.o,然后重建
  • 好的,现在可以了,感谢 4 位帮助!

标签: c module makefile linux-kernel


【解决方案1】:

好吧,你不需要在你的makefile中调用GCC来构建一个模块,试试这个makefile:

obj-m += hello.o
hello-objs := funcs.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

另外,你可以告诉模块哪个函数是入口点和出口点,以及声明模块描述、作者和许可是很好的。 (Why here)

试试这个:

static int __init enter_module(void)
{
    return 0;
}

static void __exit exit_module(void)
{
}

module_init(enter_module);
module_exit(exit_module);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("John <john@jonnyland.com>");
MODULE_DESCRIPTION("This is the module description.");

内核关键字__init__exit 用于让内核通过尽可能从内存中删除函数来进行优化。

而宏 module_initmodule_exit 将注册模块进入和退出函数。

【讨论】:

  • 改变了,结果还是一样 :(
  • @alteration 您构建的内核版本是什么?
  • @alteration 编辑了涵盖 Makefile 更改的答案。
  • 等等,显然现在它运行但不打印消息(在 dmesg 中找不到)
  • 你好:模块验证失败:缺少签名和/或所需的密钥 - 污染内核
猜你喜欢
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
  • 2012-09-27
  • 2011-11-13
  • 1970-01-01
相关资源
最近更新 更多