Makefile变量:“ MY_FILE”和“ obj-m”之间的区别

时间:2019-01-26 00:17:08

标签: c makefile linux-device-driver gnu-make

我有两个简单的程序(应用程序,驱动程序)和一个可动态编译这些应用程序的Makefile。当我运行“ make test_prg”时,它将正确编译应用程序。但是,这不适用于驱动程序“ make test_drv”。

〜/ test / test_prg / hello-world.c

#include <stdio.h>
int main(void) {
    printf("Hello world\n");
    return 0;
}

〜/ test / test_drv / hello-world.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
int __init hello_init(void) {
    printk(KERN_INFO "Hello world\n");
    return 0;
}
void __exit hello_exit(void) {
    printk(KERN_INFO "Bye world\n");
}
module_init(hello_init);
module_exit(hello_exit);

〜/ test / Makefile

MAKEFLAGS       := -B
DRV_PATH        := /lib/modules/$(shell uname -r)/build
PRG_PATH        := .

MY_PATH         := 
MY_TARGET       :=
MY_FILE         ?=

test_%: _setup_test_%
        make M=$(PWD) -C $(MY_PATH) MY_FILE=$@/$(MY_FILE) $(MY_TARGET)

build:
        gcc $(MY_FILE)

_setup_test_drv:
        $(eval MY_PATH=$(DRV_PATH))
        $(eval MY_TARGET=modules)
        $(eval obj-m=hello-world.o)
        echo $(obj-m) $(MY_PATH)

_setup_test_prg:
        $(eval MY_PATH=$(PRG_PATH))
        $(eval MY_TARGET=build)
        $(eval MY_FILE=hello-world.c)

设置了正确的值,但是找不到要编译的模块:

echo hello-world.o /lib/modules/4.6.7-pd-4.6.y/build
hello-world.o /lib/modules/4.6.7-pd-4.6.y/build
make M=/home/timgru/test -C /lib/modules/4.6.7-pd-4.6.y/build MY_FILE=test_drv/ modules
make[1]: Entering directory '/usr/src/linux-headers-4.6.7-pd-4.6.y'
  Building modules, stage 2.
  MODPOST 0 modules
make[1]: Leaving directory '/usr/src/linux-headers-4.6.7-pd-4.6.y'

奇怪的是,如果我对“驱动程序参数”进行硬编码,则说明驱动程序已正确编译:

〜/ test / test_drv / Makefile

obj-m:=hello-world.o
#hello-world-objs:=$(shell find . -name '*.c')

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

我试图了解为什么〜/ test / Makefile解决方案无法编译驱动程序。此外,我想以某种方式绕过这个问题:-)

1 个答案:

答案 0 :(得分:1)

提示:从不硬编码make在makefile中。始终使用$(MAKE)

用目标obj-m调用时,您的makefile没有定义modules。正确的是

obj-m :=
obj-m += hello-world.o

test_drv test_prg:
        $(MAKE) M=$(PWD) -C $(MY_PATH) $(MY_TARGET)

test_drv: MY_PATH   := $(DRV_PATH)
test_drv: MY_TARGET := modules

这是您的Makefile的正确版本:

MAKEFLAGS := -B
DRV_PATH  := /lib/modules/$(shell uname -r)/build
PRG_PATH  := .

.PHONY: test_drv test_prg
test_drv test_prg:
    $(MAKE) M=$(PWD) -C $(MY_PATH) MY_FILE=$@/$(MY_FILE) $(MY_TARGET)

.PHONY: build
build:
    gcc $(MY_FILE)

test_drv: MY_PATH   := $(DRV_PATH)
test_drv: MY_TARGET := modules
# input values for "modules" & friends
obj-m               :=
obj-m               += hello-world.o

test_prg: MY_PATH   := $(PRG_PATH)
test_prg: MY_TARGET := build
test_prg: MY_FILE   := hello-world.c