开发者

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百科 -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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜