There have been several questions regarding circular dependencies being dropping when running a Makefile (here and here), however, I am still a bit confused as to why they are occurring.
For example, I am trying to compile a Win32 GUI program that contains resource files (from this tutorial). These resource files are compiled to object files through the windres
command, so they can be linked to the final executable (as described here):
CC = gcc
CFLAGS = -mwindows
DEPS = resource.h
OBJ = menu_one.o $(patsubst %.rc,%.rc.o,$(wildcard *.rc))
all: menu_one
%.rc.o: %.rc
windres $^ -o $@
%.o: %.c $(DEPS)
$(CC) -c $< -o $@ $(CFLAGS)
menu_one: $(OBJ)
$(CC) $^ -o $@ $(CFLAGS)
The command $(patsubst %.rc,%.rc.o,$(wildcard *.rc))
takes all the resource files ending in .rc
and slaps a .o
extension onto them (e.g. resource.rc.o
).
When I run this, everything seems to work and I get a working executable, however, Make outputs the following:
gcc -c menu_one.c -o menu_one.o -mwindows
make: Circular menu_one.rc <- menu_one.rc.o dependency dropped.
windres menu_one.rc -o menu_one.rc.o
gcc menu_one.o menu_one.rc.o -o menu_one -mwindows
Is this circular dependency occurring because I technically have two .o
rules? In other words, how can I correct this circular dependency?
Edit 1:
I tried following what @MadScientist said, however, this still created a circular dependency with Make. After some more Googling, I came across the following page. Towards the very bottom there is a section titled "Circular File Dependencies." This got me thinking about the output from Make:
make: Circular menu_one.rc <- menu_one.rc.o dependency dropped.
It looks to be that the rc
suffix is creating this dependency - even though it is not part of the file extension of the outputted object file (i.e. file.rc.o
). If I change the output file suffix to be .res.o
, the circular dependency goes away entirely:
...
RESOBJ = $(patsubst %.rc,%.res.o,$(wildcard *.rc))
OBJ = menu_one.o $(RESOBJ)
...
%.res.o: %.rc
windres $^ -o $@
...
This sparks a very similar question, if I wanted to use the previous suffix .rc.o
, how would you accomplish this? Is it possible?
Edit 2:
@MadScientist's suggestion of using a match-anything rule worked perfectly with correcting the problem. This now allows me to use the .rc.o
suffix ending. See @MadScientist's updated answer below.