Operating System - HP-UX
1833685 Members
3917 Online
110062 Solutions
New Discussion

Re: Question about MAKE utility

 
SOLVED
Go to solution
Wojtek Dudczak
Occasional Contributor

Question about MAKE utility

Hi all,

Make has double suffix interference rule .cpp.o for building .o files from .cpp files. This rule looks like:

.cpp.o
$(CXX) $(CXXFLAGS) -c $<

But this rue produces .o file in the same directory where .cpp source file is located, and works only when .o and .cpp are in the same directory. Is it possible to define similiar rule, that will allow me to have source code and object files in separate directories?

Thank You in advance
7 REPLIES 7
Ceesjan van Hattum
Esteemed Contributor

Re: Question about MAKE utility

As i can see in 'man make', it is possible to execute multiple commands in a rule; like:

.y.o:
$(YACC) $(YFLAGS) $<
$(CC) $(CFLAGS) -c y.tab.c
rm y.tab.c
mv y.tab.o $@

So, can you try to do:
.cpp.o
$(CXX) $(CXXFLAGS) -c $<
mkdir ./newdir
mv *.o ./newdir/

Please let me know your findings..
Regards,
Ceesjan
Tom Maloy
Respected Contributor

Re: Question about MAKE utility

You may need to use nmake or gmake, or try to write some of your own inference rules.

Note that Ceesjan's solution will cause you to do a rebuild of every *.cpp file every time you run make, because make will not look in the directory where the *.o files are.

Tom
Carpe diem!
H.Merijn Brand (procura
Honored Contributor

Re: Question about MAKE utility

rules always end with a colon (':') in all versions and ports of make. I guess that your real makefile indeed has

.cpp.o:

with a colon.

A much better make is the GNU make. Binaries are available from a HP porting center or from https://www.beepz.com/personal/merijn
Enjoy, Have FUN! H.Merijn
Wojtek Dudczak
Occasional Contributor

Re: Question about MAKE utility

Ceesjan were right, but as Tom said: this solution would cause to rebuild all .cpp files every time make is launched. And of course I omitted colon in my example by mistake.

Thank you guys for your assistance.

Wojtek
Sergiy Nevstruyev
New Member

Re: Question about MAKE utility

Hi Wojtek,

you can try to use the following one. Looks ugly but works :-).

################################

OD = ./obj
OD_OBJS = $(OD)/test.o
OBJS = test.o

main: moveobjs $(OBJS) removeobjs

moveobjs:
cp $(OD_OBJS) . || echo ""

.c.o:
gcc -o $(OD)/$@ -c $<

removeobjs:
rm $(OBJS)
Ian Lochray
Respected Contributor
Solution

Re: Question about MAKE utility

Stefan Paitoni has answered this in another thread. The solution is:

VPATH=:

.cpp.o :
...
Wojtek Dudczak
Occasional Contributor

Re: Question about MAKE utility

Best solution was with VPATH

Thanks again