|
Home > Archive > Unix Programming > January 2004 > How to compile sources file and save object files into a separate directory
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to compile sources file and save object files into a separate directory
|
|
| Rainbow Fish 2004-01-23, 5:33 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.
| |
| Mark A. Odell 2004-01-23, 5:33 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 ->
--
| |
| Derk Gwen 2004-01-23, 5:33 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][color=darkred]
done
echo $executable: $ofiles \
'; $(LINK.cpp) $(DBG_OPTS) -o ' $executable $ofiles \[QUOTE][color=darkred]
make $*
--
Derk Gwen http://derkgwen.250free.com/html/index.html
You hate people.
But I love gatherings. Isn't it ironic.
|
|
|
|
|