【问题标题】:Compiling out-of-tree kernel module against any kernel source tree on the filesystem针对文件系统上的任何内核源代码树编译树外内核模块
【发布时间】:2014-04-17 13:49:14
【问题描述】:

我正在尝试针对文件系统上的任何源代码树编译一个模块,但我在使用 Makefile 时遇到了问题。这是我针对指定内核的原始 Makefile:

obj-m += new-mod.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

这个 Makefile 可以正确编译,但目标是让它针对任何源代码树进行编译。我试过了:

obj-m += new-mod.o

我认为“全部:”是假定的,但我收到错误:

make: *** No targets.  Stop.

我也加了:

all: 

到Makefile,除了错误信息没有任何区别:

make: Nothing to be done for `all'

我尝试了很多文档,但没有运气。我将不胜感激。

【问题讨论】:

  • 我也面临同样的问题,我使用了 Makefile > KVERSION=$(shell uname -r) PWD := $(shell pwd) all: make -C /lib/modules/$( KVERSION)/build/ M=$(PWD) modules clean: make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean $(info Building with KERNELRELEASE = ${KERNELRELEASE}) obj-m := assignment1.o

标签: linux build makefile kernel kernel-module


【解决方案1】:

goal is to have it compile against any source tree

你可以提供compiled source-code path

只需替换make -C /lib/modules/$(shell uname -r)/build M=$PWD modules

有了这个

make -C <path-to-compiled-src-code> M=$PWD modules

make -C /home/vinay/linux-3.9 M=$PWD modules

在makefile下面试试

Makefile——

# if KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq (${KERNELRELEASE},)
obj-m := new-mod.o
# Otherwise we were called directly from the command line.
# Invoke the kernel build system.
  else
    KERNEL_SOURCE := /usr/src/linux
    PWD := $(shell pwd)
default:
      ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

clean:
      ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif

在上面你可以把KERNEL_SOURCE := /usr/src/linux-->改成.-->你的sr-code KERNEL_SOURCE := <path to compiled-src-code>

更多信息请在下方点赞

while building kernel modules why do we need /lib/modules?

A simple program on linux device driver

How to make a built-in device driver in linux

【讨论】:

  • 如果我不知道 KERNEL_SOURCE 怎么办,因为我不知道目标系统中这个变量的值是什么,所以你能提供任何其他方法来做同样的事情吗?
  • 在 make 行添加“CC=”,如make CC=/home/myGcc -C <src> M=...
【解决方案2】:

根据您的自定义内核源(不是已安装的)构建, 您可以使用以下步骤。

1.从kernel.org下载内核(tar)

2.提取

3.make x86_64_defconfig

4.做好准备

5.make modules_prepare

现在您必须更改 Makefile 以指向您已下载和提取的内核源代码。 Vinay Answer 的例子中提到了类似的东西。

请记住,您不能 insmod 这个模块,因为内核正在运行并且构建的模块是针对不同内核的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    相关资源
    最近更新 更多