如何安排Makefile来编译具有多个.c文件的内核模块?
如何安排Makefile来编译具有多个.c文件的内核模块?
这是我当前的 Makefile。 它是由 KDevelop 自动生成的
TARGET = nlb-driver
OBJS = nlb-driver.o
MDIR = drivers/misc
EXTRA_CFLAGS = -DEXPORT_SYMTAB
CURRENT = $(shell uname -r)
KDIR = /lib/modules/$(CURRENT)/build
PWD = $(shell pwd)
DEST = /lib/modules/$(CURRENT)/kernel/$(MDIR)
obj-m += $(TARGET).o
default:
make -C $(KDIR) M=$(PWD) modules
$(TARGET).o: $(OBJS)
$(LD) $(LD_RFLAG) -r -o $@ $(OBJS)
ifneq (,$(findstring 2.4.,$(CURRENT)))
install:
su -c "cp -v $(TARGET).o $(DEST) && /sbin/depmod -a"
else
install:
su -c "cp -v $(TARGET).ko $(DEST) && /sbin/depmod -a"
endif
clean:
-rm -f *.o *.ko .*.cmd .*.flags *.mod.c
make -C $(KDIR) M=$(PWD) clean
-include $(KDIR)/Rules.make
How to arrange a Makefile to compile a kernel module with multiple .c files?
Here is my current Makefile. It was auto generated by KDevelop
TARGET = nlb-driver
OBJS = nlb-driver.o
MDIR = drivers/misc
EXTRA_CFLAGS = -DEXPORT_SYMTAB
CURRENT = $(shell uname -r)
KDIR = /lib/modules/$(CURRENT)/build
PWD = $(shell pwd)
DEST = /lib/modules/$(CURRENT)/kernel/$(MDIR)
obj-m += $(TARGET).o
default:
make -C $(KDIR) M=$(PWD) modules
$(TARGET).o: $(OBJS)
$(LD) $(LD_RFLAG) -r -o $@ $(OBJS)
ifneq (,$(findstring 2.4.,$(CURRENT)))
install:
su -c "cp -v $(TARGET).o $(DEST) && /sbin/depmod -a"
else
install:
su -c "cp -v $(TARGET).ko $(DEST) && /sbin/depmod -a"
endif
clean:
-rm -f *.o *.ko .*.cmd .*.flags *.mod.c
make -C $(KDIR) M=$(PWD) clean
-include $(KDIR)/Rules.make
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$(TARGET).o 的依赖项可以是多个目标文件,一个对应于驱动程序中的每个源文件。 许多其他驱动程序在 OBJS 初始声明后使用 += 运算符。 例如,
目标规则将扩展为
如果源文件的数量多于一行,这很好。 但如果文件数量很少,也可以在一行中定义所有对象
The dependencies for $(TARGET).o can be multiple object files, one for each source file in your driver. Many other drivers use the += operator after the initial declaration of OBJS. For example,
The target rule would then expand to be
This is nice if there are more source files than comfortably fit on a line. But if there are only a small number of files, you can also define all the objects on a single line
就我而言,该项目由 6 个文件组成:
monter_main.c
、monter_main.h
monter_cdev.c
、monter_cdev.h
monter_pci.c
,monter_pci.h
monter_main.c
是我模块的主文件。请记住,您不应拥有与您尝试构建的模块同名的文件(例如
monter.c
和monter.ko
),除非您已获取该文件中的所有代码。这是我的 Makefile:
Makefile
Kbuild
In my case the project consists of 6 files:
monter_main.c
,monter_main.h
monter_cdev.c
,monter_cdev.h
monter_pci.c
,monter_pci.h
monter_main.c
is the main file of my module.Remember that you shouldn't have a file with the same name as the module you're trying to build (e.g.
monter.c
andmonter.ko
) unless you've got all code in that one file.Here are my Makefiles:
Makefile
Kbuild
我认为只需在第二行中列出更多目标文件就可以解决问题。
I would assume that just listing more object files in the second line would do the trick.