C++ & Makefile

Andrew Dalke dalke at acm.org
Mon Jan 22 04:27:39 EST 2001

Christian Liebich wrote:
>I just begun to programm in the c++ lanuage
>could you tell me how to write a makefile, to compile and link some c++

>I have a file main.cc with the main programm inside and beside four
>files (a.cc , b.cc , a.h , b.h) with functions
>in the main.cc I include a.h and b.h.

Here is a Makefile which will do what you want, although I
haven't tested it.  Note that there is a tab character before
the "$(CCC)" text and not spaces.  The "CCC" and "CFLAGS"
definitions are there because you will likely need to change
those settings to support different platforms or to try
different optimization settings.


main.exe: main.o a.o b.o
    $(CCC) -o main.exe main.o a.o b.o

a.o: a.cc a.h
    $(CCC) $(CFLAGS) a.cc

b.o: b.cc b.h
    $(CCC) $(CFLAGS) b.cc

main.o: main.cc a.h b.h main.h
    $(CCC) $(CFLAGS) main.cc

There are more concise ways to write this Makefile
using transformation rules and allowing gcc to make the
dependencies for you.  (BTW, using "g++" as the compiler
name is, I believe, deprecated. A couple of years ago
it was changed so gcc automatically recognizes C++
programs based on the filename extension, just like it also
recognizes Fortran programs.)

The best thing to do is get the O'Reilly book on
using make, and also go through the GNU Make documentation.
If you get really into it, you can use GNU automake
and autoconf to make the makefile for you.  This is
more complicated to learn, but at the end you can
define the Makefile.am in a couple of lines that does

                    dalke at acm.org


More information about the Bio-soft mailing list

Send comments to us at biosci-help [At] net.bio.net