Linux内核模块编译

发布于 2024-09-30 04:05:56 字数 653 浏览 0 评论 0原文

我尝试编译简单的 Linux 内核模块:

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

int init_module(void)
{
        printk("Hello world 1.\n");
        return 0;
}

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

我的 makefile:

obj-m = testmodule.o
KVERSION = $(shell uname -r)
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

现在我的 .c 文件中没有错误。

但是当我尝试在终端中 make 时: make: Nothing to be do for `all'。

怎么了?

谢谢。

I try to compile simple linux kernel module:

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

int init_module(void)
{
        printk("Hello world 1.\n");
        return 0;
}

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

My makefile:

obj-m = testmodule.o
KVERSION = $(shell uname -r)
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

Now i haven't errors in my .c file.

But when i try make in terminal: make: Nothing to be done for `all'.

What's wrong?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

惜醉颜 2024-10-07 04:05:56

makefile 中的默认命令是

make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

指示 make cd 到 /lib/modules/$(KVERSION)/build 并运行

make module m=YOUR_CURRENT_DIR

反过来,该 makefile 找不到任何可做的事情。据推测,该 makefile 期望在当前目录中找到某些特定结构。

您需要更仔细地阅读引导您设置此 makefile 的所有说明。

The default command in your makefile is

make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

That instructs make to cd to /lib/modules/$(KVERSION)/build and run

make module m=YOUR_CURRENT_DIR

In turn, that makefile is not finding anything to do. Presumably, that makefile expects to find some particular structure in your current directory.

You need to more carefully read whatever instructions led you to set up this makefile.

霊感 2024-10-07 04:05:56

您确实没有提供足够的信息来确定正确答案,但我会给您一些需要研究的内容。

你如何编译它?您的构建中是否有正确的包含路径?构建路径是使用 gcc 的 -I 选项指定的。确保您指向内核源代码树。

你构建了内核吗?当您执行 make 时,会进行某些设置以允许您进行构建。执行 make 并不会构建所有内容(例如模块),但会为您提供初始设置。

You really don't give enough information to determine the correct answer, but I'll give you some things to look into.

How are you compiling it? Do you have the correct include path in your build? Build paths are specified with the -I option to gcc. Make sure you are pointing to your kernel source tree.

Have you build the kernel? When you do a make, certain things are setup that allows you to build. Doing a make doesn't build everything (like modules) but will get the initial stuff setup for you.

数理化全能战士 2024-10-07 04:05:56

确保您的发行版安装了提供这些包含文件的内核头文件/开发包。

如果已安装,请搜索这些包含文件在计算机上的位置,并将这些目录添加到编译器包含搜索路径(-I 选项)。

Make sure your have your distributions the kernel header/development packages installed that provide these include files.

If they are installed, search where these include files are located on your computer and add these directories to your compilers include search path (the -I option).

且行且努力 2024-10-07 04:05:56

这是因为你的 Makefile 在给出 make 命令之前不包含制表符

make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

所以通常你的文件应该如下所示:

obj-m = test.o                                                                   

KVERSION = $(shell uname -r)                                                     

all:                                                                             
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules                  
clean:                                                                           
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean  

This is because your Makefile contains no tab before giving make command

make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

So normally your file should be like below:

obj-m = test.o                                                                   

KVERSION = $(shell uname -r)                                                     

all:                                                                             
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules                  
clean:                                                                           
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文