如何安排一个Makefile来编译带有多个.c文件的内核模块? [英] How to arrange a Makefile to compile a kernel module with multiple .c files?

查看:623
本文介绍了如何安排一个Makefile来编译带有多个.c文件的内核模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何安排Makefile来编译带有多个.c文件的内核模块?

How to arrange a Makefile to compile a kernel module with multiple .c files?

这是我当前的Makefile.它是由 KDevelop

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

推荐答案

在我的情况下,该项目包含6个文件:

In my case the project consists of 6 files:

  • monter_main.cmonter_main.h
  • monter_cdev.cmonter_cdev.h
  • monter_pci.cmonter_pci.h
  • monter_main.c, monter_main.h
  • monter_cdev.c, monter_cdev.h
  • monter_pci.c, monter_pci.h

monter_main.c是我模块的主文件.

monter_main.c is the main file of my module.

请记住,除非您在该文件中包含所有代码,否则您不应拥有与要构建的模块同名的文件(例如monter.cmonter.ko).

Remember that you shouldn't have a file with the same name as the module you're trying to build (e.g. monter.c and monter.ko) unless you've got all code in that one file.

这是我的Makefile:

Here are my Makefiles:

  • Makefile

KDIR ?= /lib/modules/`uname -r`/build

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

install:
    $(MAKE) -C $(KDIR) M=$$PWD modules_install

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

  • Kbuild

    obj-m := monter.o
    monter-objs := monter_main.o monter_cdev.o monter_pci.o
    

  • 这篇关于如何安排一个Makefile来编译带有多个.c文件的内核模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆