我仍在学习Makefile,但我在理解如何正确使用它时遇到了问题.
我有一个简单的项目, struct 如下:
我想使用我的Makefile
来编译子目录Mcal
和Ecual
中的所有源文件,而不需要为使用的每个.c
文件重复代码.
我当前的Makefile代码是:
#Directories
#Files
FILENAME = blink
#Flags
MCU = atmega16a
WFLAGS = -Wall -Wextra -Werror -Wshadow
#Toolchain
COMPILE = avr-gcc -mmcu=$(MCU) -Os $(WFLAGS)
LINK = avr-gcc -mmcu=$(MCU)
# Linking
$(FILENAME): main.o Led.o Dio.o
$(LINK) $^ -o $(FILENAME)
#Compiling
main.o: main.c
$(COMPILE) -c $^ -o $@
Led.o: Ecual/Led/Led.c
$(COMPILE) -c $^ -o $@
Dio.o: Mcal/Dio/Dio.c
$(COMPILE) -c $^ -o $@
我使用了以下代码,但仅当所有源文件都位于根文件中时才起作用:
#Compiling
%.o: %.c
$(COMPILE) -c $^ -o $@