Makefile question - Unix

This is a discussion on Makefile question - Unix ; 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, ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Makefile question

  1. Makefile question

    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!

  2. Re: Makefile question

    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

  3. Re: Makefile question

    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
    g++ -o test test.o C1.o C2.o

    test.o: test.cpp C1.o C2.o
    g++ -o test.o test.cpp

    C1.o: C1.cpp C1.h B.o
    g++ -o C1.o C1.cpp

    C2.o: C2.cpp C2.h B.o
    g++ -o C2.o C2.cpp

    A.o: A.cpp A.h
    g++ A.cpp

    B.o: B.cpp B.h A.o
    g++ B.cpp

  4. Re: Makefile question

    On 10 Jan, 16:55, xz 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.




+ Reply to Thread