This error usually means you're missing a -c
when compiling a simple program, something like this:
%.o: %.c
$(CC) $(CFLAGS) -o $@ $^
Program: main.o
$(CC) $(LDFLAGS) -o $@ $^
What's happening is that the first rule is building main.o
just like you asked, but instead of being an object file, it's actually the complete, compiled and linked program.
When the second rule tries to use it as an object file, the linker finds that it's not an object file at all and produces "can't link with a main executable file."
Obviously for a more complex program, one with multiple object files or with library dependencies, it would not be able to build an executable from just the one source file, so you'll get a different error and never get as far as the link rule.
The solution, of course, is to add -c
to the first rule so that the first invocation only compiles and does not link, producing an actual object file.
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $^