Makefile and Wildcards

Ok, this is my current setup for the makefile. There are files called public01.c , public02.c , etc. I am trying to create object files for each of them using the public*.o label with a wildcard.

 public*.o: public*.c hashtable.h $(CC) $(CFLAGS) -c public*.c public*: public*.o $(CC) -o public* public*.o 

However, when I try to run the makefile, I get the following:

 make: *** No rule to make target `public*.c', needed by `public*.o'. Stop. 

I assume it treats public*.c as a label, not a wildcard, as I would like. I read about the function $(wildcard pattern...) and played with it, but I really did not understand it or did not get it to work ...

+4
source share
2 answers

Short answer : this syntax does not work the way you want. The right way to do what you want in GNU, to do the syntax, is to use template rules:

 public%.o: public%.c hashtable.h $(CC) $(CFLAGS) -c $< public%: public%.o $(CC) -o $@ $< 

Long answer : This:

 public*.o: public*.c hashtable.h 

doesn't mean what you want it to mean. Assuming you have several public01.c files, etc., and without public01.o files, etc., at the beginning of your build, this syntax is equivalent to this:

 public*.o: public01.c public02.c public03.c ... hashtable.h 

That is, if public01.o , etc. does not exist, then make will use the literal string public*.o as the file name. If some of the .o files exist, then this syntax is equivalent to this:

 public01.o public02.o ...: public01.c public02.c ... hashtable.h 

It seems that you want the right? But this is a common misunderstanding with make, because in fact this line is exactly the same:

 public01.o: public01.c public02.c ... hashtable.h public02.o: public01.c public02.c ... hashtable.h 

That is, each .o file has a dependency on each .c ! The right way to do what you want is to use a template rule as shown above.

+12
source

This error usually occurs because gcc considers the file does not exist. Make sure it exists and make sure that you are in the directory where it exists, and then it should work.

Also, for any reason, you don’t have $ (CFLAGS) for the public *? Why is this publicly available *? I just think that "public" is enough.

0
source

All Articles