找到你要的答案

Q:Kernel module makefile output name

Q:内核模块的Makefile文件输出名称

I am trying to compile my kernel module. The .c file name is file1.c, but I need the .ko file name to be mod1.ko.

How can I do that?

My current makefile:

obj-m := mod1.o
KDIR :=/lib/modules/$(shell uname -r)/build
PDW := $(shell pwd)

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

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

I am trying to compile my kernel module. The .c file name is file1.c, but I need the .ko file name to be mod1.ko.

我该怎么做?

我目前的makefile:

obj-m := mod1.o
KDIR :=/lib/modules/$(shell uname -r)/build
PDW := $(shell pwd)

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

clean:
    $(MAKE) -C $(KDIR) M=$(PDW) clean
answer1: 回答1:

You should change your first line to something like this:

obj-m += module_name.o
module_name-y := file1.o

Where module_name is the module file name (in this example the output will be module_name.ko) and it will be build from file1.c. You can add more than one source file to the 2nd line, so it could be:

module_name-y := file1.o file2.o file3.o

In this case module_name.ko will be build from file1.c, file2.c and file3.c.

You should read this document if you want to fully understand Linux kernel makefiles. Your problem is described somewhere around line 190.

你应该把你的第一行改成这样:

obj-m += module_name.o
module_name-y := file1.o

在module_name是模块的文件名(在这个例子中的输出将module_name。KO),它将建立从1。你可以添加多个源文件的第二行,所以它可以:

module_name-y := file1.o file2.o file3.o

在这种情况下module_name.ko将建立从1。C,C和C源文件,源文件。

你应该充分理解Linux内核的Makefile读取这个文件。你的问题描述在190号线的某个地方。

answer2: 回答2:

The solution looks like this:

obj-m += mod1.o
mod1-objs := file1.o

KBUILD_CPPFLAGS += -I$(PWD)/

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

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

解决方案看起来像这样:

obj-m += mod1.o
mod1-objs := file1.o

KBUILD_CPPFLAGS += -I$(PWD)/

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

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