I'm trying to build a simple project to learn autotools and automake. I have a subdir called Atmega-lib which build a static library called libatmega328p.a. The probem I'm facing it that when I run make flash
i get the following output :
make: *** No rule to make target 'Atmega-lib/libatmega328p.a', needed by 'atmega'. Stop.
But, running make
first and then make flash
works. How can I make it so that running only make flash
would build eveything and then attempt to run the rule ?
Here is my Makefile :
SUBDIRS = Atmega-lib
#SUBDIRS = Atmega-lib
AM_CFLAGS = -I$(SUBDIRS)/include -Wall -Wextra -std=c89 -g -Wpadded -Wconversion -fno-common -mmcu=atmega328p -Wl,--gc-sections -Wshadow -Wdouble-promotion -Wundef
# -nostdlib
OUTPUT_HEX = atmega328p.hex
bin_PROGRAMS = atmega
atmega_LDADD = Atmega-lib/libatmega328p.a
atmega_SOURCES = main.c
# Add a custom target for flashing
flash: atmega Atmega-lib/libatmega328p.a
@echo "Flashing the ATmega328P..."
avrdude -F -V -p m328p -c arduino -P /dev/ttyACM0 -b 115200 -U flash:w:$(OUTPUT_HEX):i
# Rule to create the hex file using avr-objcopy
$(OUTPUT_HEX): atmega
avr-objcopy -O ihex -R .eeprom $< $(OUTPUT_HEX)
The thing I need to tell to automake is to first run SUBDIRS before buildingthe atega program exactly how it does when running make
alone but I don't know how.
EDIT:
I added this line right after the flash rule and it workd. Was it the right way to solve my issue ?
# Add a custom target for flashing
flash:
$(MAKE)
@echo "Flashing the ATmega328P..."
avrdude -F -V -p m328p -c arduino -P /dev/ttyACM0 -b 115200 -U flash:w:$(OUTPUT_HEX):i
Thank you in advance !
How can I make it so that running only make flash would build eveything and then attempt to run the rule ?
Simplest solution: make the rule for flash
have (only) all
as a prerequisite. That maps exactly to "ensure every (default) thing is up to date before running the recipe". Especially since flash
is a synthetic target.
The issue here is that each makefile stands on its own, so the root-level makefile does not have any knowledge about the rules in the subdirectory makefile, such as the one for Atmega-lib/libatmega328p.a
. The main targets generated by Automake do recursion (all
, clean
, etc), but you don't automatically get recursion for arbitrary targets.
You should also consider the issues discussed in the classic paper Recursive make considered harmful. Automake can readily be used to produce non-recursive build systems, for any source layout, and using it that way is my typical recommendation. That, too, could resolve your issue, and I would consider it a superior solution.