1

I have just started learning make and I am having some trouble. I want to create an executable called sortfile. Here are the relevant files: sortfile.c fileio.c fileio.h

Both sortfile.c and fileio.c use fileio.h.

Here is my makefile:

 1 CC = gcc 2 CFLAGS = -g -Wall 3 4 default: sortfile 5 6 sortfile: sortfile.o fileio.o $(CC) $(CFLAGS) -o sortfile sortfile.o fileio.o 7 8 sortfile.o: sortfile.c fileio.h $(CC) $(CFLAGS) -c sortfile.c 9 10 fileio.o: fileio.c fileio.h $(CC) $(CFLAGS) -c fileio.c 11 12 clean: 13 $(RM) sortfile *.o*~ 

I am getting the following error:

make: *** No rule to make target `gcc', needed by `sortfile.o'. Stop. 

1 Answer 1

3

Makefiles are of the format:

target: dependency1 dependency2 rule (command to build) 

You listed the command as the set of dependencies, so make wants to try to build gcc before it can expect sortfile.o to be satisfied.

Instead of:

sortfile.o: sortfile.c fileio.h $(CC) $(CFLAGS) -c sortfile.c 

you need:

sortfile.o: sortfile.c fileio.h $(CC) $(CFLAGS) -c sortfile.c 

Note that normally all you really want here is something much simpler:

CFLAGS+=-g -Wall all: sortfile sortfile: sortfile.o fileio.o 

This brief Makefile will work as expected with GNU Make because of implicit rules (see also).

Sign up to request clarification or add additional context in comments.

2 Comments

Inserting a semicolon between the end of the prerequisite list and before the command will also work (i.e. sortfile: sortfile.o fileio.o ; $(CC) $(CFLAGS) -o sortfile sortfile.o fileio.o).
If we run out of carriage returns, that could be really handy! ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.