Makefile非递归方法问题



我正试图根据中收到的指导,使用非递归方法,使用代码结构下方的Makefile进行构建

链路描述

work
├── code
|     | 
|     └── main.h and test.h files here
│     └── main.c and test.c files here
|     └── subdir.mk
|
├── _Build/
│     └── Makefile here

下面是Makefile

-include ../code/subdir.mk
all : target_file
target_file : ../code/main.o ../code/test.o
@echo Building ...
@echo Linking files ...
gcc -Llib ../code/main.o ../code/test.o -lm -o target_file  
clean:
rm -rv ../code/*.o

下面是子dr.mk文件

../code/test.o : ../code/test.c ../code/test.h
@echo Building test.c ...
gcc -Werror -Wall -c ../code/test.c -o ../code/test.o
../code/main.o : ../code/main.c ../code/main.h ../code/test.h
@echo Building main.c ...
gcc -Werror -Wall -c ../code/main.c -o ../code/main.o

运行make命令时得到的输出低于

Building test.c ...
gcc -Werror -Wall -c ../code/test.c -o ../code/test.o

我没有得到错误,也没有main。o正在生成。此外,在Makefile中,链接命令不会执行。

这可能是因为make默认情况下会构建它遇到的第一个目标。由于在all : target_file行之前包含了subdir.mk,因此将生成在subdir.mk中命名的第一个目标,而不生成其他目标。解决方案:将subdir.mk夹杂物放在最后,例如靠近末端。

最新更新