Makefile 变量:"MY_FILE" 和 "obj-m" 之间的区别
Makefile variables: difference between "MY_FILE" and "obj-m"
我有两个简单的程序(应用程序、驱动程序)和一个动态编译这些应用程序的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'
奇怪的是,如果我对 "driver parameters" 进行硬编码,驱动程序将被正确编译:
~/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 编译驱动程序的解决方案不起作用。此外,我想以某种方式绕过这个问题:-)
提示:永远不要在生成文件中硬编码make
。始终使用 $(MAKE)
.
当使用目标 modules
调用时,您的 makefile 没有定义 obj-m
。正确的是
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
我有两个简单的程序(应用程序、驱动程序)和一个动态编译这些应用程序的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'
奇怪的是,如果我对 "driver parameters" 进行硬编码,驱动程序将被正确编译:
~/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 编译驱动程序的解决方案不起作用。此外,我想以某种方式绕过这个问题:-)
提示:永远不要在生成文件中硬编码make
。始终使用 $(MAKE)
.
当使用目标 modules
调用时,您的 makefile 没有定义 obj-m
。正确的是
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