1

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??

1 Answers1

1

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.

  1. Build them here and move them by hand (this one will almost certainly get you in trouble):
    $(OBJECTS): %.o: %.c
        gcc -c $&lt -o $@
        mv $@ $(OBJDIR)
    
    or equivalently, go there and build them:
    $(OBJECTS): %.o: %.c
        cd $(OBJDIR) ; gcc -c $&lt -o $@
    
  2. Specify the path and build them in place (probably the best for a beginner):
    FULLOBJECTS = $(addprefix $(OBJDIR)/,$(OBJECTS))
    $(FULLOBJECTS): $(OBJDIR)/%.o: %.c
        gcc -c $&lt -o $@
    
  3. Call Make there (powerful, but very tricky):
    $(OBJECTS):
        $(MAKE) -C $(OBJDIR) $@
    
Beta
  • 96,650
  • 16
  • 149
  • 150
  • 1
    I was trying your second suggestion. However I could not get that to work. How would I use the FULLOBJECTS? 'make FULLOBJECTS'? – thoni56 Jul 23 '10 at 10:26
  • Ah, I see that I made a mistake in my answer: the target should be `$(FULLOBJECTS)`, not `FULLOBJECTS`. I will correct that. If you still have trouble, post your makefile. – Beta Jul 23 '10 at 12:24