gpt4 book ai didi

在 Linux 中使用 makefile 编译内核模块

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:30:02 27 4
gpt4 key购买 nike

我是内核编程的新手。我写了一个 hello world 程序,但我无法执行它。我已经搜索并在/usr/src 下制作了一个 make 文件,然后执行 sudo make 命令来运行它。但它给出了以下错误:

make -C /lib/modules/3.2.0-23-generic-pae/build M=/usr/src modules
make[1]: Entering directory `/usr/src/linux-headers-3.2.0-23-generic-pae'
make[2]: *** No rule to make target `/usr/src/hello.c', needed by `/usr/src /hello.o'. Stop.
make[1]: *** [_module_/usr/src] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.2.0-23-generic-pae'
make: *** [all] Error 2

制作文件:

obj-m += hello.o

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

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

hello.c(在 ~Desktop/inet/中)

#include <linux/module.h>   
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");

/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}

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

在 makefile 中,我注意每一行只有一个制表符空间!

请大家说说是什么问题!

最佳答案

将您的 Makefile 移动到模块源代码 hello.c 所在的位置。这意味着在 ~/Desktop/inet/ 中。看起来您已将 Makefile 放入 /usr/src/

$ sudo mv /usr/src/Makefile ~/Desktop/inet/
$ cd ~/Desktop/inet/
$ make

关于在 Linux 中使用 makefile 编译内核模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28900117/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com