[geeks] Makefile question

Jonathan C Patschke jp at celestrion.net
Sat Nov 9 02:12:35 CST 2002


On Saturday, November 9, 2002, at 01:00 AM, Joshua D Boyd wrote:

> I've been going back and forth oer the gnu make manual on the gnu.org
> web site, and I just can't figure out how to modify my Makefile to 
> allow
> parallel compilation with make -j2.

It should Just Work.

>   Here is the simple Makefile.
>
> CC = g++;
>
> muopengl.a: *.o
> 	    $(CC) -shared *.o -o muopengl.a
> 	    mv muopengl.a ../mulib/
> 	    cp include/*.h ../mulib/include/
>
> *.o:
>      $(CC) -c src/*.cpp -Iinclude -I../mulib/include -I../mulib
>
> .PHONY: clean
> clean:
> 	rm *.o

That Makefile doesn't contain any parallelizable steps, since you're 
building all the files as one dependency.  What you'll want to do is 
make a list of your .o files (since *.o doesn't mean anything to Make 
when no .o files exist!) like this:

OBJS = foo.o bar.o baz.o bot.o

Then:

muopengl.a: $(OBJS)
	$(CXX) -shared $(OBJS) -o ../mulib/muopengl.a

Now, for to build each OBJ file, make sure that CXX, CPPFLAGS and 
CXXFLAGS are set properly:

# Note: Traditionally, CXX is the C++ compiler, and CC is the C 
compiler.
CXX = g++
CXXFLAGS = -g -O
CPPFLAGS = -Iinclude -I../mulib/include -I../mulib

And create an implicit rule to translate .cpp files into .o files 
(Actually, later versions of GNU Make may support .cpp in addition to 
.cc and .C):

%.o: %.cpp
	$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $<

Now, insert the rules for each .o file:

foo.o: foo.cpp

bar.o: bar.cpp

baz.o: baz.cpp

bot.o: bat.cpp

Now, the steps for building each of your .o files, and your .a file are 
all separate, and gmake will know how to parallelize them.

--
Jonathan C. Patschke
Celestrion Information Systems
Thorndale, TX



More information about the geeks mailing list