How to compile sources file and save object files into a separate directory
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > How to compile sources file and save object files into a separate directory




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    How to compile sources file and save object files into a separate directory  
Rainbow Fish


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:21 PM

Hi there,

I am building a make file to compile many .cpp files in different
directories, and save .o files into one directory. All source file
names are different.

For example:
/prj/cmn/cmn.cpp
/prj/prj1/prj1.cpp
/prj/prj1/main.cpp

Save .o files to
/out/prj1/cmn.o
/out/prj1/prj1.o
/out/prj1/main.o

I trid to do it like this,

SOURCES := /prj/cmn/cmn.cpp /prj/prj1/prj1.cpp /prj/prj1/main.cpp
OBJECTS := $(addprefix $(OBJDIR)/, $(addsuffix .o, $(basename $(notdir
$(SOURCES)))))

all: /out/prj1/prj1

/out/prj1/prj1 : $(OBJECTS)
$(LINK.cpp) $(DBG_OPTS) -o /out/prj1/prj1 $(OBJECTS)

/out/prj1/%.o : %.cpp
$(COMPILE.cpp) $(DBG_OPTS) -o $@ $<

But it does not work.
Please help me. Thanks in advance.





[ Post a follow-up to this message ]



    Re: How to compile sources file and save object files into a separate directory  
Mark A. Odell


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:21 PM

rainbowfish888@yahoo.com (Rainbow Fish) wrote in
news:1e996f7d.0312290719.56d74edb@posting.google.com:
quote:
> I am building a make file to compile many .cpp files in different > directories, and save .o files into one directory. All source file > names are different. > > For example: > /prj/cmn/cmn.cpp > /prj/prj1/prj1.cpp > /prj/prj1/main.cpp > > Save .o files to > /out/prj1/cmn.o > /out/prj1/prj1.o > /out/prj1/main.o > > I trid to do it like this, > > SOURCES := /prj/cmn/cmn.cpp /prj/prj1/prj1.cpp /prj/prj1/main.cpp > OBJECTS := $(addprefix $(OBJDIR)/, $(addsuffix .o, $(basename $(notdir > $(SOURCES)))))
Use VPATH for to tell make where to find the sources. Then just set SOURCES to the names of the files w/o the path. E.g. VPATH=/prj/cmn;/prj/prj1 Getting the .o files to go into some other directory, I have only done by running make on the makefile in the dir that the .o files are to go into. I'd like to see how to do this correctly too. -- - Mark -> --




[ Post a follow-up to this message ]



    Re: How to compile sources file and save object files into a separate directory  
Derk Gwen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:21 PM

# I am building a make file to compile many .cpp files in different
# directories, and save .o files into one directory. All source file
# names are different.

I usually handle this in one of two ways, neither of which is very popular
because they don't depend on using every possible trick gnu make provides.

Solution 1: Go ahead and enumerate all the targets.

all: /out/prj1/prj1
/out/prj1/prj1: /out/prj1/cmn.o /out/prj1/prj1.o /out/prj1/main.o
$(LINK.cpp) $(DBG_OPTS) -o /out/prj1/prj1 \
/out/prj1/cmn.o /out/prj1/prj1.o /out/prj1/main.o
/out/prj1/cmn.o: /prj/cmn/cmn.cpp
$(COMPILE.cpp) $(DBG_OPTS) -o /out/prj1/cmn.o /prj/cmn/cmn.cpp
/out/prj1/prj1.o: /prj/prj1/prj1.cpp
$(COMPILE.cpp) $(DBG_OPTS) -o /out/prj1/prj1.o /prj/prj1/prj1.cpp
/out/prj1/main.o: /prj/prj1/main.cpp
$(COMPILE.cpp) $(DBG_OPTS) -o /out/prj1/main.o /prj/prj1/main.cpp


Solution 2: Use a script to create the makefile and do the make

#!/bin/sh
sourceroot=$1
objroot=$2
executable=$3
shift 3
echo all: $executable >makefile
ofiles=""
for cfile in $(find $sourceroot -name '*.cpp' -print)
do
ofile=$objroot/$(basename -s .cpp $cfile).o
ofiles="$ofiles $ofile"
echo $ofile: $cfile \
'; $(COMPILE.cpp) $(DBG_OPTS) -o' $ofile $cfile\[QUOTE] 
done
echo $executable: $ofiles \
'; $(LINK.cpp) $(DBG_OPTS) -o ' $executable $ofiles \[QUOTE] 
make $*

--
Derk Gwen http://derkgwen.250free.com/html/index.html
You hate people.
But I love gatherings. Isn't it ironic.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:29 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register