VPATH in makefile issue
I have a question related to VPATH. I'm playing around with make files and the VPATH variable. Basically, I'm grabbing source files from a few different places (specified by the VPATH), and trying 开发者_开发技巧to compile them into the another directory ( $CURDIR/OBJ/ ) using simply a list of .o-files that I want.
Can I create .o's in any another directory other than current dir when using VPATH??
Well, yes, but your question isn't quite right.
Make is good at finding things there and using them to make things here, but not the other way around. VPATH and vpath tell make where to look for the things it needs, but there is no corresponding directive to put the things it makes somewhere other than the current directory.
If you want your object files (OBJECTS = ant.o bee.o) to go into another directory like $(OBJDIR), there are basically three ways to do it.
- Build them here and move them by hand (this one will almost certainly get you in trouble):
$(OBJECTS): %.o: %.c gcc -c $< -o $@ mv $@ $(OBJDIR)
or equivalently, go there and build them:$(OBJECTS): %.o: %.c cd $(OBJDIR) ; gcc -c $< -o $@
- Specify the path and build them in place (probably the best for a beginner):
FULLOBJECTS = $(addprefix $(OBJDIR)/,$(OBJECTS)) $(FULLOBJECTS): $(OBJDIR)/%.o: %.c gcc -c $< -o $@
- Call Make there (powerful, but very tricky):
$(OBJECTS): $(MAKE) -C $(OBJDIR) $@
精彩评论