4

我已经尝试了几个小时来创建我的第一个 Hello World Kernel 模块,可惜没有成功。我的简单 C 代码 (hello.c) 和 makefile 位于我系统上的 /Downloads 中,如果这很重要的话。

你好ç

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

MODULE_LICENSE("Dual BSD/GPL");

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

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

module_init(hello_init);
module_exit(hello_exit);

生成文件

obj−m += hello.o

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

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

这是我尝试通过在终端中输入“make”(不带引号)来构建内核模块时收到的错误消息:

make -C /lib/modules/4.2.0-16-generic/build
M=/home/username/workspace/test/Module modules make[1]:
Entering directory '/usr/src/linux-headers-4.2.0-16-generic'
scripts/Makefile.build:44:
/home/username/workspace/test/Module/Makefile: No such file or
directory make[2]: *** No rule to make target
'/home/username/workspace/test/Module/Makefile'.  Stop.
Makefile:1398: recipe for target
'_module_/home/username/workspace/test/Module' failed make[1]:
*** [_module_/home/username/workspace/test/Module] Error 2 make[1]: Leaving directory '/usr/src/linux-headers-4.2.0-16-generic'
makefile:4: recipe for target 'all' failed make: *** [all] Error 2

我已经尝试了几乎每一个解决方案,并遵循了可以在谷歌上找到的关于这个问题的每一个建议。没有什么可以解决我的问题......如果有人知道我可以尝试的任何事情,我将非常感激!

  • 我应该将我的 hello.c 和 makefile 移动到 /usr/src 吗?
  • 我的文件中是否有任何语法错误?
  • 它应该在makefile中说hello.c而不是hello.o吗?
  • 我的 Ubuntu 可能会丢失一些东西吗?
  • ...
4

1 回答 1

0

我刚刚将名称“makefile”更改为“Makefile”(大写 M),这似乎做了一些事情。现在我得到:

make -C /lib/modules/4.2.0-16-generic/build M=/home/username/workspace/test/Module modules make[1]: 进入目录'/usr/src/linux-headers-4.2.0 -16-generic' 构建模块,第 2 阶段。MODPOST 0 模块 make[1]:离开目录 '/usr/src/linux-headers-4.2.0-16-generic'

^^ 我从一开始就知道我的 makefile 导致了这个问题!尽管如此,我仍然没有 hello.ko 文件。

有什么建议我现在应该尝试吗?

我的天啊 !!!

谷歌搜索了这个新问题并从 dsf 找到了这个答案:Linux:modpost 没有构建任何东西(最后发布)

正如他们建议的那样,我手动在 Makefile 中键入文本,然后,就像变魔术一样,它工作得非常好!现在我有了我的 hello.ko,但不幸的是,没有任何解释。也许,如果您只是从 pdf/网站复制 Makefile 文本,您最终会得到不需要的字符,正如 dsf 似乎相信的那样?!

总之,问题解决了。

于 2015-10-28T19:24:53.547 回答