Makefile question #3

  • Follow


I am learning how to deal with Makefile and got so confused.
Could anyone be so kind and give a little sample Makefile for the
following particular example?

Let's say I have the following files describing 4 classes (A, B, C1,
C2) and a test program (test.cpp) which runs a test for classes C1 and
C2.

A.h
A.cpp
B.h
B.cpp
C1.h
C1.cpp
C2.h
C2.cpp
test.cpp

The inhirentance relation among the 4 classes are as follows:

class C1: public B
class C2: public B
class B: public A

i.e. C1 and C1 are both dependent on B while B is dependent on A.

test.cpp depends on C1 and C2 and contains the main() function.

I am on a linux machine(Ubuntu 7.10) and use g++ 4.1 as the compiler.

Then how should I write the Makefile? Thanks a lot in advance!
0
Reply zhang.xi.cn (102) 1/10/2008 2:23:34 AM

xz wrote:
> I am learning how to deal with Makefile and got so confused.
> Could anyone be so kind and give a little sample Makefile for the
> following particular example?
> 
> Let's say I have the following files describing 4 classes (A, B, C1,
> C2) and a test program (test.cpp) which runs a test for classes C1 and
> C2.
> 
> A.h
> A.cpp
> B.h
> B.cpp
> C1.h
> C1.cpp
> C2.h
> C2.cpp
> test.cpp
> 
> The inhirentance relation among the 4 classes are as follows:
> 
> class C1: public B
> class C2: public B
> class B: public A
> 
> i.e. C1 and C1 are both dependent on B while B is dependent on A.
> 
> test.cpp depends on C1 and C2 and contains the main() function.
> 
> I am on a linux machine(Ubuntu 7.10) and use g++ 4.1 as the compiler.
> 
> Then how should I write the Makefile? Thanks a lot in advance!

Class inheritance doesn't matter at all.  You just need to define
the interfaces in the .h files and put the code in the .cpp files.
Then, you need to compile the .cpp files.  Since each .cpp file is
built independently of the other and depends only on the header
files, it does not matter what order they are compiled in.

Ideally you should put the dependencies (including transitive
dependencies) for every file in its line in the makefile.  But
there are tools to generate those dependencies for you and
write them into the makefile.

   - Logan
0
Reply lshaw-usenet (926) 1/10/2008 5:28:08 AM


I have always wondered how to express dependencies such as object
definition ones, where inheritance is involved.

Here is my first go at writing that simple makefile; it assumes you
have one class definition per .cpp file.
The dependency from an object (compiled) file ensures any interface
change in the base class is reflected in the derived class (so C1.o
depends on B.o, because if either B.h or A.h are changed, you need to
get C1.o recompiled: B.o will be recompiled whether B.h or A.h are
changed anyway).

test: test.o C1.o C2.o
<tab/>g++ -o test test.o C1.o C2.o

test.o: test.cpp C1.o C2.o
<tab/>g++ -o test.o test.cpp

C1.o: C1.cpp C1.h B.o
<tab/>g++ -o C1.o C1.cpp

C2.o: C2.cpp C2.h B.o
<tab/>g++ -o C2.o C2.cpp

A.o: A.cpp A.h
<tab/>g++ A.cpp

B.o: B.cpp B.h A.o
<tab/>g++ B.cpp
0
Reply f.e.negroni (57) 1/10/2008 10:32:13 AM

On Jan 10, 4:32 am, fnegroni <f.e.negr...@googlemail.com> wrote:
> I have always wondered how to express dependencies such as object
> definition ones, where inheritance is involved.
>
> Here is my first go at writing that simple makefile; it assumes you
> have one class definition per .cpp file.
> The dependency from an object (compiled) file ensures any interface
> change in the base class is reflected in the derived class (so C1.o
> depends on B.o, because if either B.h or A.h are changed, you need to
> get C1.o recompiled: B.o will be recompiled whether B.h or A.h are
> changed anyway).
>
> test: test.o C1.o C2.o
> <tab/>g++ -o test test.o C1.o C2.o
>
> test.o: test.cpp C1.o C2.o
> <tab/>g++ -o test.o test.cpp
>
> C1.o: C1.cpp C1.h B.o
> <tab/>g++ -o C1.o C1.cpp
>
> C2.o: C2.cpp C2.h B.o
> <tab/>g++ -o C2.o C2.cpp
>
> A.o: A.cpp A.h
> <tab/>g++ A.cpp
>
> B.o: B.cpp B.h A.o
> <tab/>g++ B.cpp

Thanks for the reply.

But wouldn't this Makefile get an error saying that B.cpp (and A.cpp,
etc.) does not have a main() function?

I actually did the similar thing and got this error msg.
0
Reply zhang.xi.cn (102) 1/10/2008 4:55:28 PM

On Jan 10, 4:32 am, fnegroni <f.e.negr...@googlemail.com> wrote:
> I have always wondered how to express dependencies such as object
> definition ones, where inheritance is involved.
>
> Here is my first go at writing that simple makefile; it assumes you
> have one class definition per .cpp file.
> The dependency from an object (compiled) file ensures any interface
> change in the base class is reflected in the derived class (so C1.o
> depends on B.o, because if either B.h or A.h are changed, you need to
> get C1.o recompiled: B.o will be recompiled whether B.h or A.h are
> changed anyway).
>
> test: test.o C1.o C2.o
> <tab/>g++ -o test test.o C1.o C2.o
>
> test.o: test.cpp C1.o C2.o
> <tab/>g++ -o test.o test.cpp
>
> C1.o: C1.cpp C1.h B.o
> <tab/>g++ -o C1.o C1.cpp
>
> C2.o: C2.cpp C2.h B.o
> <tab/>g++ -o C2.o C2.cpp
>
> A.o: A.cpp A.h
> <tab/>g++ A.cpp
>
> B.o: B.cpp B.h A.o
> <tab/>g++ B.cpp

Thanks for the reply.

But wouldn't this Makefile get an error saying that B.cpp (and A.cpp,
etc.) does not have a main() function?

I actually did the similar thing and got this error msg.
0
Reply zhang.xi.cn (102) 1/10/2008 4:55:46 PM

On Jan 10, 8:55=A0am, xz <zhang.xi...@gmail.com> wrote:
> On Jan 10, 4:32 am, fnegroni <f.e.negr...@googlemail.com> wrote:
>
>
>
>
>
> > I have always wondered how to express dependencies such as object
> > definition ones, where inheritance is involved.
>
> > Here is my first go at writing that simple makefile; it assumes you
> > have one class definition per .cpp file.
> > The dependency from an object (compiled) file ensures any interface
> > change in the base class is reflected in the derived class (so C1.o
> > depends on B.o, because if either B.h or A.h are changed, you need to
> > get C1.o recompiled: B.o will be recompiled whether B.h or A.h are
> > changed anyway).
>
> > test: test.o C1.o C2.o
> > <tab/>g++ -o test test.o C1.o C2.o
>
> > test.o: test.cpp C1.o C2.o
> > <tab/>g++ -o test.o test.cpp
>
> > C1.o: C1.cpp C1.h B.o
> > <tab/>g++ -o C1.o C1.cpp
>
> > C2.o: C2.cpp C2.h B.o
> > <tab/>g++ -o C2.o C2.cpp
>
> > A.o: A.cpp A.h
> > <tab/>g++ A.cpp
>
> > B.o: B.cpp B.h A.o
> > <tab/>g++ B.cpp
>
> Thanks for the reply.
>
> But wouldn't this Makefile get an error saying that B.cpp (and A.cpp,
> etc.) does not have a main() function?
>
> I actually did the similar thing and got this error msg.- Hide quoted text=
 -
>
> - Show quoted text -

You need all of the object files in the final link. And do not name
your program "test".
That name conflicts with the shell built-in named 'test'.

PROGRAM =3D MyProgram
OBJS =3D test.o   A.o   B.o   C.o
LIBRARIES =3D < list of needed system libraries, such as: -lm >
CFLAGS =3D <compiler flags, such as -g for debug, etc.>

$(PROGRAM):: $(OBJS)
<tab> g++ -o $@ $(OBJS)  $(LIBRARIES)

=2Ec.o:
<tab>g++  -c $(CFLAGS) $*.c

--
Fred Kleinschmidt
0
Reply fred.l.kleinschmidt (236) 1/10/2008 6:13:06 PM

On Jan 10, 10:13=A0am, fred.l.kleinschm...@boeing.com wrote:
> On Jan 10, 8:55=A0am, xz <zhang.xi...@gmail.com> wrote:
>
>
>
>
>
> > On Jan 10, 4:32 am, fnegroni <f.e.negr...@googlemail.com> wrote:
>
> > > I have always wondered how to express dependencies such as object
> > > definition ones, where inheritance is involved.
>
> > > Here is my first go at writing that simple makefile; it assumes you
> > > have one class definition per .cpp file.
> > > The dependency from an object (compiled) file ensures any interface
> > > change in the base class is reflected in the derived class (so C1.o
> > > depends on B.o, because if either B.h or A.h are changed, you need to
> > > get C1.o recompiled: B.o will be recompiled whether B.h or A.h are
> > > changed anyway).
>
> > > test: test.o C1.o C2.o
> > > <tab/>g++ -o test test.o C1.o C2.o
>
> > > test.o: test.cpp C1.o C2.o
> > > <tab/>g++ -o test.o test.cpp
>
> > > C1.o: C1.cpp C1.h B.o
> > > <tab/>g++ -o C1.o C1.cpp
>
> > > C2.o: C2.cpp C2.h B.o
> > > <tab/>g++ -o C2.o C2.cpp
>
> > > A.o: A.cpp A.h
> > > <tab/>g++ A.cpp
>
> > > B.o: B.cpp B.h A.o
> > > <tab/>g++ B.cpp
>
> > Thanks for the reply.
>
> > But wouldn't this Makefile get an error saying that B.cpp (and A.cpp,
> > etc.) does not have a main() function?
>
> > I actually did the similar thing and got this error msg.- Hide quoted te=
xt -
>
> > - Show quoted text -
>
> You need all of the object files in the final link. And do not name
> your program "test".
> That name conflicts with the shell built-in named 'test'.
>
> PROGRAM =3D MyProgram
> OBJS =3D test.o =A0 A.o =A0 B.o =A0 C.o
> LIBRARIES =3D < list of needed system libraries, such as: -lm >
> CFLAGS =3D <compiler flags, such as -g for debug, etc.>
>
> $(PROGRAM):: $(OBJS)
> <tab> g++ -o $@ $(OBJS) =A0$(LIBRARIES)
>
> .c.o:
> <tab>g++ =A0-c $(CFLAGS) $*.c
>
> --
> Fred Kleinschmidt- Hide quoted text -
>
> - Show quoted text -

Should be
=2Ecpp.o:
not
=2Ec:.o:
--
Fred Kleinschmidt
0
Reply fred.l.kleinschmidt (236) 1/10/2008 6:22:08 PM

On 10 Jan, 16:55, xz <zhang.xi...@gmail.com> wrote:
> Thanks for the reply.
>
> But wouldn't this Makefile get an error saying that B.cpp (and A.cpp,
> etc.) does not have a main() function?
>
> I actually did the similar thing and got this error msg.

I forgot to put '-c' between g++ and the name of the cpp file.
It won't complain about a missing main when compiling .cpp into .o,
you only need a main() when linking all the .o together to form an
executable.



0
Reply f.e.negroni (57) 1/10/2008 10:39:16 PM

7 Replies
29 Views

(page loaded in 0.074 seconds)


Reply: