【问题标题】:Build Linux Kernel Module using Makefile with different pathname使用具有不同路径名的 Makefile 构建 Linux 内核模块
【发布时间】:2016-05-31 18:33:18
【问题描述】:

我正在尝试使用Linux Kernel Module Programming Guide 中指定的标准示例 Makefile 编译 Linux 内核模块。如果 Makefile 被称为Makefile,那么一切正常。如果我将 Makefile 重命名为 Makefile.hello 或其他名称,则会失败,因为它找不到路径 Makefile。我想知道是否有一个命令或一组标志可以添加到我的 Makefile 中以正确执行此功能。我需要重命名 Makefile,因为我从 CMake 调用它。 Cmake 创建自己的 Makefiles 并且通常会覆盖我已经拥有的文件。

我用 hello world 示例替换了我的内核模块代码并复制了问题。我知道它的makefile。

你好世界示例hello.c

/*
 * hello−1.c − The simplest kernel module.
 */
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
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");
}

生成文件

obj-m += hello.o

ifeq (,$(KDIR))
    KDIR := /lib/modules/$(shell uname -r)/build
endif

PWD := $(shell pwd)

all:
    $(MAKE) -C $(KDIR) M=$(PWD) $(KCONFIG) modules

clean:
    $(MAKE) -C $(KDIR) M=$(PWD) clean

如果 makefile 被称为Makefile。 (成功构建)

$> make -f Makefile
make -C /lib/modules/4.4.0-21-generic/build M=/home/msmith/Desktop/kernel-test  modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-21-generic'
  CC [M]  /home/msmith/Desktop/kernel-test/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/msmith/Desktop/kernel-test/hello.mod.o
  LD [M]  /home/msmith/Desktop/kernel-test/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-21-generic'

如果 makefile 被调用 Makefile.hello 则输出(构建失败)

$> make -f Makefile.hello
make -C /lib/modules/4.4.0-21-generic/build M=/home/msmith/Desktop/kernel-test  modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-21-generic'
scripts/Makefile.build:44: /home/msmith/Desktop/kernel-test/Makefile: No such file or directory
make[2]: *** No rule to make target '/home/msmith/Desktop/kernel-test/Makefile'.  Stop.
Makefile:1396: recipe for target '_module_/home/msmith/Desktop/kernel-test' failed
make[1]: *** [_module_/home/msmith/Desktop/kernel-test] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-21-generic'
Makefile.hello:10: recipe for target 'all' failed
make: *** [all] Error 2

我尝试将 -f 添加到内部 MAKE 参数中,但这只会导致更多问题。

【问题讨论】:

标签: c linux makefile linux-kernel


【解决方案1】:

将所有与 Kbuild 相关的逻辑移动到文件 Kbuild 中。内核的构建系统首先检查具有此名称的文件,因此它不会查看由 CMake 创建的Makefile。此功能记录在 Documentation/kbuild/makefiles.txt 中。

我在与 Linux 内核相关的 CMake 项目中正是使用这种方法。

【讨论】:

  • 解决了。感谢您的帮助。像魅力一样工作。
【解决方案2】:

打开脚本/Makefile.build 进入内核树:

 41 # The filename Kbuild has precedence over Makefile  
 42 kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))  
 43 kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)  
 44 include $(kbuild-file)

这部分代码 (43-44) 包含名为“Makefile”的 Makefile。

【讨论】:

    【解决方案3】:

    默认的 Makefile。名称 = Makefile ... 和 Makefile-hello,或 Makefile.hello。

    obj-m    := hello.o
    
    KDIR    := /lib/modules/$(shell uname -r)/build
    PWD    := $(shell pwd)
    
    default:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
    
    clean:
        $(MAKE) -C $(KDIR) M=$(PWD) clean
    

    $make:好的

    $make -f Makefile-hello好的

    $make -f Makefile.hello 也可以。

    你的 Makefile : $ make -f Makefile.msmith 好的,没有错误。


    【讨论】:

      猜你喜欢
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多