user982740
user982740

Reputation: 203

VPATH not wotking in a makefile

I have a very small make file with content

VPATH = src

main: main.o
        gcc -o main main.o

main.o: main.c
        gcc -c main.c

Current directory contains a directory src which contains main.c

When I execute make, I get error

gcc -c main.c
gcc: main.c: No such file or directory
gcc: no input files
make: *** [main.o] Error 1

When I move main.c in the current directory, it works. It seems VPATH macro is not working. Please let me know the usage of VPATH.

Upvotes: 2

Views: 2450

Answers (1)

thiton
thiton

Reputation: 36049

While make locates main.c just fine, you are missing the use of the automatic variables here:

main.o : main.c
        gcc -o $@ -c $<

which will be expanded by make to the gcc call

gcc -o main.o -c src/main.c

Always use the automatic variables $< (first prerequisite) and $@ (target) where you can, they make make both more powerful and easier to read.

Upvotes: 6

Related Questions