c - Makefile 变量:“MY_FILE”和“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'
奇怪的是,如果我硬编码“驱动程序参数”,驱动程序正在正确编译:
~/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
在 makefile 中硬编码。始终使用$(MAKE)
.
您的 makefile 未定义obj-m
何时使用 target 调用它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
推荐阅读
- sql - 计算 GUID 列的所有子项
- dart - Column 内展开的小部件未展开,而是不可见
- android - 请求网络设备的mac地址
- android - 具有 EditText 的 TableRow 在键盘消失之前不显示文本
- android - 如何修复 atp0000 错误,我在构建项目时收到?
- android - dataSnapshot 需要声明为 final
- amazon-web-services - 使用 Google 的 Cloud HSM 签署可执行文件
- list - 如何编写自己的“清单”?Scheme中的程序
- python - 无法将文件重命名为自定义名称
- pointers - 对内存块中的变量求和