JoshPark
JoshPark

Reputation: 29

C Makefile, Strange error with "No target" when a target is present

So I have the makefile

HEADERS = preferences.h distances.h
OBJECTS = movie_recommender.o preferences.o distances.o

default: movie_recommender

%.o: %.c $(HEADERS)
    ; gcc -c $< -o $@

movie_recommender: $(OBJECTS)
    ; gcc $(OBJECTS) -o $@

clean:
    ; -rm -f $(OBJECTS)
    ; -rm -f movie_recommender

And I receive the error:

No rule to make target 'movie_recommender.o', needed by 'movie_recommender'. Stop.

File paths I've tried adding the target,

movie_recommender.o: movie_recommender.c
    gcc -c movie_recommender.c

And I receive another error. Could anyone please tell me what I'm doing wrong? Many thanks!

Upvotes: 1

Views: 395

Answers (1)

Daniel H
Daniel H

Reputation: 7443

The make program only looks in the current directory for your files. There is no movie_recommender.c file in the current directory, so make doesn't see it and doesn't find any way to make movie_recommender.o.

The solution is to add make your makefile aware of the source and header directories, which you can do with the vpath directive. If you add

vpath %.c src

then it will automatically look for %.c files in the src directory.

Similarly, make won't find your headers.

GCC also won't find the headers, and it can't know about make's vpath, so instead you need to pass -Iinclude to GCC.

Upvotes: 3

Related Questions