Make a Makefile

Discussion in 'C Programming' started by Paminu, Feb 11, 2006.

  1. Paminu

    Paminu Guest

    I have the following three files:

    link.h
    contains my structs and list of functions.

    link.c
    contains my implementation of the functions

    linktest.c
    contains my main function that test the program with some values.


    If I only make changes in linktest.c there would be no reason to recompile
    link.c

    The first time I compile the whole program I do:

    gcc -c link.c
    gcc -o linktest linktest.c tree.o


    But when I only edit the linktest.c file there is no reason to run the first
    line: "gcc -c link.c"

    Is it possible to make that kind of "rule" in a makefile?


    I have tried to write the follwoing make file:

    treetest: tree.c
    gcc link.c -c
    gcc -o linktest linktest.c link.o


    But this way both the gcc commands gets run each time.
    Paminu, Feb 11, 2006
    #1
    1. Advertising

  2. On Sat, 11 Feb 2006 21:16:00 +0100, Paminu wrote:

    > treetest: tree.c
    > gcc link.c -c
    > gcc -o linktest linktest.c link.o

    Try this instead:

    OBJ = link.o linktest.o

    all: treetest

    treetest: $(OBJ)
    gcc $(OBJ) -o treetest

    link.o: link.c link.h
    gcc -c link.c link.h

    linktest.o: linktest.c
    gcc -c linktest.c
    --
    Hilsen/Regards
    Michael Rasmussen
    http://keyserver.veridis.com:11371/pks/lookup?op=get&search=0xE3E80917
    Michael Rasmussen, Feb 11, 2006
    #2
    1. Advertising

  3. On Sat, 11 Feb 2006 21:16:00 +0100, Paminu wrote:

    >
    > But this way both the gcc commands gets run each time.

    Try this instead:

    OBJ = link.o linktest.o

    all: treetest

    treetest: $(OBJ)
    gcc $(OBJ) -o treetest

    link.o: link.c link.h
    gcc -c link.c link.h

    linktest.o: linktest.c
    gcc -c linktest.c

    --
    Hilsen/Regards
    Michael Rasmussen
    http://keyserver.veridis.com:11371/pks/lookup?op=get&search=0xE3E80917
    Michael Rasmussen, Feb 11, 2006
    #3
  4. Paminu

    Joe Wright Guest

    Michael Rasmussen wrote:
    > On Sat, 11 Feb 2006 21:16:00 +0100, Paminu wrote:
    >
    >
    >>treetest: tree.c
    >> gcc link.c -c
    >> gcc -o linktest linktest.c link.o

    >
    > Try this instead:
    >
    > OBJ = link.o linktest.o
    >
    > all: treetest
    >
    > treetest: $(OBJ)
    > gcc $(OBJ) -o treetest
    >
    > link.o: link.c link.h
    > gcc -c link.c link.h
    >
    > linktest.o: linktest.c
    > gcc -c linktest.c


    Really? 'gcc -c link.c link.h' compiling a header? Surely link.c will
    have '#include "link.h"' and if not, why not?

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Feb 11, 2006
    #4
  5. Paminu <> writes:
    > I have the following three files:
    >
    > link.h
    > contains my structs and list of functions.
    >
    > link.c
    > contains my implementation of the functions
    >
    > linktest.c
    > contains my main function that test the program with some values.

    [snip]
    > Is it possible to make that kind of "rule" in a makefile?


    This is not a C question. The "make" command comes with
    documentation; you should find the answer there. Failing that, try
    comp.unix.programmer.

    (Quick hint: the answer is yes.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 11, 2006
    #5
  6. Joe Wright <> writes:
    > Michael Rasmussen wrote:
    >> On Sat, 11 Feb 2006 21:16:00 +0100, Paminu wrote:
    >>
    >>>treetest: tree.c
    >>> gcc link.c -c
    >>> gcc -o linktest linktest.c link.o

    >> Try this instead:
    >> OBJ = link.o linktest.o
    >> all: treetest
    >> treetest: $(OBJ)
    >> gcc $(OBJ) -o treetest
    >> link.o: link.c link.h
    >> gcc -c link.c link.h
    >> linktest.o: linktest.c
    >> gcc -c linktest.c

    >
    > Really? 'gcc -c link.c link.h' compiling a header? Surely link.c will
    > have '#include "link.h"' and if not, why not?


    And this kind of thing is *exactly* why we discourage answers to
    off-topic questions here. (Actually it's only one of a number of
    reasons.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 11, 2006
    #6
  7. Paminu

    Flash Gordon Guest

    Michael Rasmussen wrote:
    > On Sat, 11 Feb 2006 21:16:00 +0100, Paminu wrote:
    >
    >> But this way both the gcc commands gets run each time.

    > Try this instead:
    >
    > OBJ = link.o linktest.o


    <snip>

    Can you please take your discussion of Makefiles somewhere appropriate.
    Possibly comp.programmer, although I am not certain about that, check
    their FAQ and a sample of posts first. Makefiles are not, and never have
    been, part of C.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Feb 11, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MACEI'S
    Replies:
    0
    Views:
    1,337
    MACEI'S
    Jul 10, 2003
  2. Karthik
    Replies:
    0
    Views:
    395
    Karthik
    Apr 29, 2004
  3. Replies:
    4
    Views:
    802
  4. Jeff
    Replies:
    7
    Views:
    1,515
    santosh
    Mar 7, 2008
  5. eric
    Replies:
    2
    Views:
    623
Loading...

Share This Page