views:

436

answers:

2

Hi

I am using gcc -MM to generate prerequisites of dependencies rules for compiling source files into object files. I want to specify a directory to hold object files and executable in the value of some variable BIN_DIR=bin/release/. There are two cases:


CASE 1

%.d: *.h *.cc Makefile
     @$(CXX) -MM $(CXXFLAGS) *.cc > $@.$$$$; \
     sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; $(RM) -rf $@.$$$$

Though unfamiliar to sed, I learned that the purpose of the sed utility is to translate (for example):

main.o : main.cc defs.h

into:

main.o main.d : main.cc defs.h

My question is how to change it further to: bin/release/main.o bin/release/main.d : main.cc defs.h

If someone could explain the usage of sed here, that would be greatly appreciated!


CASE 2

Makefile.depend: *.h *.cc Makefile
    $(CXX) -MM $(CXXFLAGS) *.cc > Makefile.depend

In the file Makefile.depend, you will see each rule is like this example:

main.o : main.cc defs.h

How to change it to be:

bin/release/main.o : main.cc defs.h

Thanks and regards!


NOTE:

I don't know why I cannot comment now. So I put my update here:

VPATH is to specify the directory of source code. But then you have to run make -f path2Makefile from where you want the object files be. Here I'd like to be able to run make -f path2 Makefile from anywhere. So it is prefered to specify directory for object files.


To Beta:

Thanks for your help! Please excuse me for not being able to comment on your answer. The problem with your method on second case is that after the command, some rules with several lines will recieve the prefix at every single line. e.g.

main.o : main.cc defs.h \

src/misc.h

becomes:

$(BIN_DIR)main.o : main.cc defs.h \

$(BIN_DIR) src/misc.h

A: 

Look up 'vpath' in the gnu.org make manual. I think it's what you need.

Jay
+1  A: 
Beta