每次从一个目录更改到另一个目录并从那里调用 make 时,目标文件都会被编译

Object files are getting compiled every time when changing from one directory to the other and invoke make from there

下面是我的测试项目的详细信息

myproject/
|
|- Makefile1
|- test_module.c
|- test/
     |- Makefile2

下面是我的 make 文件的内容

Makefile 1:

obj-m:=test_module.o
all:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules;
clean:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean;

Makefile2:
obj-m:=../test_module.o
all:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules;
clean:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean;

当我在 myproject 目录中调用 make 时,test_module.o 正在编译。如果我 cd 到 test/ 目录并调用 make,test_module.o 将再次被编译。那不是我预期的行为。 Make 不应该再次编译 test_module.o 因为它已经编译过了。它应该使用该目标文件来生成相应的可执行文件或内核模块。我想念这里的任何东西吗?

是的,在 Linux 内核构建系统中,目标文件不会被重新用于 不同的 目标(例如,内核模块)。

这是由与目标文件一起存储的辅助构建文件引起的。例如,在您的情况下 .test_module.o.cmd 使用到 test_module.o 的确切路径。当文件由 Makefile1 创建时,此路径以 test_module.o 结尾,但当 Makefile2 有问题时,路径以 test/../test_module.o.

结尾