Tell me about makefiles

Discussion in 'C Programming' started by Tomás Ó hÉilidhe, Mar 7, 2008.

  1. I already understand how program compilation works (i.e. the
    preprocessor produces individual translation units which get compiled
    separately, and then the linker links the object files together), but
    I don't know anything about makefiles.

    If I had the following program:

    /* main.c */

    extern void Func(void);

    int main(void)
    {
    Func();
    return 0;
    }

    /* func.c */

    #include <stdio.h>

    void Func(void) { puts("Hello World!"); }

    /* End of code */

    , then I would compile it as follows with gcc:

    gcc a.c b.c -ansi -pedantic -s -O3 -D NDEBUG -o prog.exe

    What would my makefile for this look like?
    Tomás Ó hÉilidhe, Mar 7, 2008
    #1
    1. Advertising

  2. Tomás Ó hÉilidhe

    Eric Sosman Guest

    Tomás Ó hÉilidhe wrote:
    > I already understand how program compilation works (i.e. the
    > preprocessor produces individual translation units which get compiled
    > separately, and then the linker links the object files together), but
    > I don't know anything about makefiles.
    >
    > If I had the following program:
    > [...]
    > What would my makefile for this look like?


    It would look like something better discussed on
    comp.unix.programmer than here.

    --
    Eric Sosman
    lid
    Eric Sosman, Mar 7, 2008
    #2
    1. Advertising

  3. Tomás Ó hÉilidhe said:

    <snip>

    > gcc a.c b.c -ansi -pedantic -s -O3 -D NDEBUG -o prog.exe
    >
    > What would my makefile for this look like?


    I don't know, but mine would look like this:

    CC=gcc
    CFLAGS=-W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
    -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
    -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
    -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
    -ffloat-store -O2
    DFLAGS=-g -pg
    a: a.o b.o
    $(CC) $(CFLAGS) $(DFLAGS) -o prog.exe a.o b.o
    a.o: a.c
    $(CC) $(CFLAGS) $(DFLAGS) -c -o a.o a.c
    b.o: b.c
    $(CC) $(CFLAGS) $(DFLAGS) -c -o b.o b.c

    clean:
    rm a.o
    rm b.o
    rm prog.exe

    install:
    cp prog.exe /usr/local/bin


    Note that, in a "real" makefile, the indented lines start with a HARD TAB
    (ASCII 9) character.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 7, 2008
    #3
  4. Eric Sosman said:

    > Tomás Ó hÉilidhe wrote:
    >> What would my makefile for this look like?

    >
    > It would look like something better discussed on
    > comp.unix.programmer than here.


    What makes you think he's using Unix?

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 7, 2008
    #4
  5. Tomás Ó hÉilidhe

    santosh Guest

    Tomás Ó hÉilidhe wrote:

    >
    > I already understand how program compilation works (i.e. the
    > preprocessor produces individual translation units which get compiled
    > separately, and then the linker links the object files together), but
    > I don't know anything about makefiles.
    >
    > If I had the following program:
    >
    > /* main.c */
    >
    > extern void Func(void);


    This is not related to your question, but isn't the above declaration
    unnecessary seeing as functions have external linkage unless specified
    otherwise?

    > int main(void)
    > {
    > Func();
    > return 0;
    > }
    >
    > /* func.c */
    >
    > #include <stdio.h>
    >
    > void Func(void) { puts("Hello World!"); }
    >
    > /* End of code */
    >
    > , then I would compile it as follows with gcc:
    >
    > gcc a.c b.c -ansi -pedantic -s -O3 -D NDEBUG -o prog.exe
    >
    > What would my makefile for this look like?
    santosh, Mar 7, 2008
    #5
  6. Tomás Ó hÉilidhe

    Ian Collins Guest

    santosh wrote:
    > Tomás Ó hÉilidhe wrote:
    >
    >> I already understand how program compilation works (i.e. the
    >> preprocessor produces individual translation units which get compiled
    >> separately, and then the linker links the object files together), but
    >> I don't know anything about makefiles.
    >>
    >> If I had the following program:
    >>
    >> /* main.c */
    >>
    >> extern void Func(void);

    >
    > This is not related to your question, but isn't the above declaration
    > unnecessary seeing as functions have external linkage unless specified
    > otherwise?


    No, if you look again, he's defining the function in another compilation
    unit.

    --
    Ian Collins.
    Ian Collins, Mar 7, 2008
    #6
  7. On Sat, 08 Mar 2008 03:13:56 +0530, santosh wrote:
    > Tomás Ó hÉilidhe wrote:
    >> I already understand how program compilation works (i.e. the
    >> preprocessor produces individual translation units which get compiled
    >> separately, and then the linker links the object files together), but I
    >> don't know anything about makefiles.
    >>
    >> If I had the following program:
    >>
    >> /* main.c */
    >>
    >> extern void Func(void);

    >
    > This is not related to your question, but isn't the above declaration
    > unnecessary seeing as functions have external linkage unless specified
    > otherwise?


    The declaration is necessary. The extern keyword in the declaration is
    unnecessary. (I'm sure that's what you meant.)

    However, some coding styles do use the extern keyword to declare functions
    that are defined in a different unit, in the same way that the keyword is
    used to declare objects that are defined in a different unit. It's not
    required, but it's perfectly valid C, and no less readable than its
    shortened equivalent.
    Harald van Dijk, Mar 7, 2008
    #7
  8. Tomás Ó hÉilidhe

    santosh Guest

    Harald van D?k wrote:

    > On Sat, 08 Mar 2008 03:13:56 +0530, santosh wrote:
    >> Tomás Ó hÉilidhe wrote:
    >>> I already understand how program compilation works (i.e. the
    >>> preprocessor produces individual translation units which get
    >>> compiled separately, and then the linker links the object files
    >>> together), but I don't know anything about makefiles.
    >>>
    >>> If I had the following program:
    >>>
    >>> /* main.c */
    >>>
    >>> extern void Func(void);

    >>
    >> This is not related to your question, but isn't the above declaration
    >> unnecessary seeing as functions have external linkage unless
    >> specified otherwise?

    >
    > The declaration is necessary. The extern keyword in the declaration is
    > unnecessary. (I'm sure that's what you meant.)


    Yes, sorry, that's what I meant. It's very late here.

    > However, some coding styles do use the extern keyword to declare
    > functions that are defined in a different unit, in the same way that
    > the keyword is used to declare objects that are defined in a different
    > unit. It's not required, but it's perfectly valid C, and no less
    > readable than its shortened equivalent.


    I see. Thanks.
    santosh, Mar 7, 2008
    #8
  9. Tomás Ó hÉilidhe

    John Bode Guest

    On Mar 7, 4:25 pm, Tomás Ó hÉilidhe <> wrote:
    > I already understand how program compilation works (i.e. the
    > preprocessor produces individual translation units which get compiled
    > separately, and then the linker links the object files together), but
    > I don't know anything about makefiles.
    >
    > If I had the following program:
    >
    > /* main.c */
    >
    > extern void Func(void);
    >
    > int main(void)
    > {
    > Func();
    > return 0;
    >
    > }
    >
    > /* func.c */
    >
    > #include <stdio.h>
    >
    > void Func(void) { puts("Hello World!"); }
    >
    > /* End of code */
    >
    > , then I would compile it as follows with gcc:
    >
    > gcc a.c b.c -ansi -pedantic -s -O3 -D NDEBUG -o prog.exe
    >
    > What would my makefile for this look like?


    The make utility and makefiles are not part of C, and are technically
    off-topic for c.l.c.

    The gnu make user guide is online at http://www.cs.utah.edu/dept/old/texinfo/make/make_toc.html.

    As for what it would look like, that kind of depends on what you're
    trying to accomplish. You can have one as simple as

    prog.exe:
    gcc a.c b.c -ansi -pedantic -s -O3 -DNDEBUG -o $@

    although that's not terribly flexible, and will build both a.c and b.c
    regardless of whether or not they're up to date with respect to the
    target.

    This is a little more sophisticated, and more typical (assumes gnu
    make):

    CC=gcc
    CFLAGS=-ansi -pedantic -c # compilation flags
    OFLAGS=-O3 # optimization flags
    AFLAGS=-DNDEBUG # assert flags

    SOURCE=a.c b.c
    OBJECTS=${SOURCE:.c=.o}
    TARGET=prog.exe

    #
    # Compile an individual .c file, creating an object (.o) file
    #
    %.o : %.c
    ${CC} -c ${CFLAGS} ${AFLAGS} ${OFLAGS} $<

    #
    # Link all the objects into the target
    #
    ${TARGET} : ${OBJECTS}
    ${CC} -o $@ ${OBJECTS}

    Remember that the command lines *must* start with a tab character,
    otherwise you'll get a less-then-helpful error message.
    John Bode, Mar 7, 2008
    #9
  10. On 7 Mar 2008 at 21:34, Richard Heathfield wrote:
    > Tomás Ó hÉilidhe said:
    >
    ><snip>
    >
    >> gcc a.c b.c -ansi -pedantic -s -O3 -D NDEBUG -o prog.exe
    >>
    >> What would my makefile for this look like?

    >
    > I don't know, but mine would look like this:
    >
    > CC=gcc
    > CFLAGS=-W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
    > -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
    > -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
    > -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
    > -ffloat-store -O2
    > DFLAGS=-g -pg
    > a: a.o b.o
    > $(CC) $(CFLAGS) $(DFLAGS) -o prog.exe a.o b.o
    > a.o: a.c
    > $(CC) $(CFLAGS) $(DFLAGS) -c -o a.o a.c
    > b.o: b.c
    > $(CC) $(CFLAGS) $(DFLAGS) -c -o b.o b.c
    >
    > clean:
    > rm a.o
    > rm b.o
    > rm prog.exe
    >
    > install:
    > cp prog.exe /usr/local/bin


    Is this a joke? That's one of the most amateurish makefiles I've ever
    seen.
    Antoninus Twink, Mar 7, 2008
    #10
  11. "Tomás Ó hÉilidhe" wrote:

    > I don't know anything about makefiles ...
    > ... What would my makefile for this look like? ...


    Off-topic here; ask this question in the following newsgroup:

    gnu.utils.help

    Also read the O'Reilly book "Managing Projects with Gnu Make"
    (available at computer stores, bookstores, and amazon.com).

    Also try these googles:

    gnu make
    makefile

    --
    Cheers,
    Robbie Hatley
    lonewolf aatt well dott com
    www dott well dott com slant user slant lonewolf slant
    Robbie Hatley, Mar 7, 2008
    #11
  12. On Fri, 7 Mar 2008 23:46:34 +0100 (CET), Antoninus Twink
    <> wrote:

    >On 7 Mar 2008 at 21:34, Richard Heathfield wrote:
    >> Tomás Ó hÉilidhe said:
    >>
    >><snip>
    >>
    >>> gcc a.c b.c -ansi -pedantic -s -O3 -D NDEBUG -o prog.exe
    >>>
    >>> What would my makefile for this look like?

    >>
    >> I don't know, but mine would look like this:
    >>
    >> CC=gcc
    >> CFLAGS=-W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
    >> -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
    >> -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
    >> -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
    >> -ffloat-store -O2
    >> DFLAGS=-g -pg
    >> a: a.o b.o
    >> $(CC) $(CFLAGS) $(DFLAGS) -o prog.exe a.o b.o
    >> a.o: a.c
    >> $(CC) $(CFLAGS) $(DFLAGS) -c -o a.o a.c
    >> b.o: b.c
    >> $(CC) $(CFLAGS) $(DFLAGS) -c -o b.o b.c
    >>
    >> clean:
    >> rm a.o
    >> rm b.o
    >> rm prog.exe
    >>
    >> install:
    >> cp prog.exe /usr/local/bin

    >
    >Is this a joke? That's one of the most amateurish makefiles I've ever
    >seen.
    >


    Is it now? I'm willing to have my mind illuminated. Do tell,
    why is it "most amateruish"? Don't just make snarky comments,
    explain yourself.



    Richard Harter,
    http://home.tiac.net/~cri, http://www.varinoma.com
    Save the Earth now!!
    It's the only planet with chocolate.
    Richard Harter, Mar 7, 2008
    #12
  13. Tomás Ó hÉilidhe

    Ian Collins Guest

    Robbie Hatley wrote:
    > "Tomás Ó hÉilidhe" wrote:
    >
    >> I don't know anything about makefiles ...
    >> ... What would my makefile for this look like? ...

    >
    > Off-topic here; ask this question in the following newsgroup:
    >
    > gnu.utils.help
    >

    Why do you assume the OP wants to use the GNU dialect of make?

    --
    Ian Collins.
    Ian Collins, Mar 7, 2008
    #13
  14. On 7 Mar 2008 at 23:29, Richard Harter wrote:
    > On Fri, 7 Mar 2008 23:46:34 +0100 (CET), Antoninus Twink
    ><> wrote:
    >
    >>On 7 Mar 2008 at 21:34, Richard Heathfield wrote:
    >>> Tomás Ó hÉilidhe said:
    >>>
    >>><snip>
    >>>
    >>>> gcc a.c b.c -ansi -pedantic -s -O3 -D NDEBUG -o prog.exe
    >>>>
    >>>> What would my makefile for this look like?
    >>>
    >>> I don't know, but mine would look like this:
    >>>
    >>> CC=gcc
    >>> CFLAGS=-W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
    >>> -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
    >>> -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
    >>> -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
    >>> -ffloat-store -O2
    >>> DFLAGS=-g -pg
    >>> a: a.o b.o
    >>> $(CC) $(CFLAGS) $(DFLAGS) -o prog.exe a.o b.o
    >>> a.o: a.c
    >>> $(CC) $(CFLAGS) $(DFLAGS) -c -o a.o a.c
    >>> b.o: b.c
    >>> $(CC) $(CFLAGS) $(DFLAGS) -c -o b.o b.c
    >>>
    >>> clean:
    >>> rm a.o
    >>> rm b.o
    >>> rm prog.exe
    >>>
    >>> install:
    >>> cp prog.exe /usr/local/bin

    >>
    >>Is this a joke? That's one of the most amateurish makefiles I've ever
    >>seen.
    >>

    >
    > Is it now? I'm willing to have my mind illuminated. Do tell,
    > why is it "most amateruish"? Don't just make snarky comments,
    > explain yourself.


    To take the most trivial points:
    1) doesn't take advantage of built-in rules
    2) highly non-extensible - should define, say, $(OBJECTS) instead of
    listing all the object files in more than one place.
    3) should rm -f so that "make clean" doesn't return a non-zero value
    when called with no built files present
    4) "make install" fails to set permissions sensibly (most likely 755
    will be wanted for a program in /usr/local/bin) - and it would be nice
    to have a $(PREFIX) variable instead of hard-coding the path
    5) WTF is "DFLAGS"? A non-standard variable name, with no comment
    explaining what it's for.
    Antoninus Twink, Mar 7, 2008
    #14
  15. In article <>,
    Antoninus Twink <> wrote:

    >>>> DFLAGS=-g -pg


    >5) WTF is "DFLAGS"? A non-standard variable name, with no comment
    >explaining what it's for.


    It's obviously "debug flags".

    -- Richard



    --
    :wq
    Richard Tobin, Mar 7, 2008
    #15
  16. Tomás Ó hÉilidhe

    Default User Guest

    Richard Harter wrote:

    > On Fri, 7 Mar 2008 23:46:34 +0100 (CET), Antoninus Twink
    > <> wrote:


    > > Is this a joke?


    > Is it now?



    Twink is troll. More specfically, he is one that is netstalking Mr.
    Heathfield. Killfile or ignore, is my recommendation.





    Brian
    Default User, Mar 8, 2008
    #16
  17. Tomás Ó hÉilidhe

    CBFalconer Guest

    Richard Harter wrote:
    > Antoninus Twink <> wrote:
    >> Richard Heathfield wrote:
    >>

    .... snip ...
    >>
    >>> I don't know, but mine would look like this:
    >>>
    >>> CC=gcc
    >>> CFLAGS=-W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
    >>> -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
    >>> -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
    >>> -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
    >>> -ffloat-store -O2
    >>> DFLAGS=-g -pg
    >>> a: a.o b.o
    >>> $(CC) $(CFLAGS) $(DFLAGS) -o prog.exe a.o b.o
    >>> a.o: a.c
    >>> $(CC) $(CFLAGS) $(DFLAGS) -c -o a.o a.c
    >>> b.o: b.c
    >>> $(CC) $(CFLAGS) $(DFLAGS) -c -o b.o b.c
    >>>
    >>> clean:
    >>> rm a.o
    >>> rm b.o
    >>> rm prog.exe
    >>>
    >>> install:
    >>> cp prog.exe /usr/local/bin

    >>
    >> Is this a joke? That's one of the most amateurish makefiles
    >> I've ever seen.

    >
    > Is it now? I'm willing to have my mind illuminated. Do tell,
    > why is it "most amateruish"? Don't just make snarky comments,
    > explain yourself.


    You realize you are talking to a prime troll?

    At any rate, it is a fine example. The only criticism I would make
    is that the dependencies for a.o and b.o do not include a.h and
    b.h.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 8, 2008
    #17
  18. Ian Collins wrote:

    > Robbie Hatley wrote:
    > > "Tomás Ó hÉilidhe" wrote:
    > >
    > >> I don't know anything about makefiles ...
    > >> ... What would my makefile for this look like? ...

    > >
    > > Off-topic here; ask this question in the following newsgroup:
    > >
    > > gnu.utils.help
    > >

    > Why do you assume the OP wants to use the GNU dialect of make?


    Why did you finally stop beating your wife after all these years?

    In other words, "Mu.".

    In still further words, it's best to avoid including (usually
    false) statements as part of questions.

    The GNU dialect of Make is close enough to the Unix dialect
    that a good knowledge of one will be of great help in using
    the other. (Yes, yes, there's differences. Deal with it.)

    I'd send the person to the following group:

    unix.utils.help

    except for one very minor technical difficulty. (Namely, no
    such group exists.)

    I probably should have mentioned these, though:

    comp.os.unix.misc
    comp.unix.misc
    comp.unix.programmer

    --
    Cheers,
    Robbie Hatley
    lonewolf aatt well dott com
    www dott well dott com slant user slant lonewolf slant
    Robbie Hatley, Mar 8, 2008
    #18
  19. In article <>,
    Robbie Hatley <> wrote:

    >> > Off-topic here; ask this question in the following newsgroup:
    >> >
    >> > gnu.utils.help


    >> Why do you assume the OP wants to use the GNU dialect of make?


    >The GNU dialect of Make is close enough to the Unix dialect
    >that a good knowledge of one will be of great help in using
    >the other.


    Just as well no-one in other newsgroups uses that kind of reasoning
    to send people here!

    -- Richard
    --
    :wq
    Richard Tobin, Mar 8, 2008
    #19
  20. "Richard Tobin" wrote:

    > In article <>,
    > Robbie Hatley <> wrote:
    >
    > >> > Off-topic here; ask this question in the following newsgroup:
    > >> >
    > >> > gnu.utils.help

    >
    > >> Why do you assume the OP wants to use the GNU dialect of make?

    >
    > >The GNU dialect of Make is close enough to the Unix dialect
    > >that a good knowledge of one will be of great help in using
    > >the other.

    >
    > Just as well no-one in other newsgroups uses that kind of reasoning
    > to send people here!


    Sure they do. Notice that we don't have 3 different newsgroups for
    original-K&R-C, C90, and C99. There's just "comp.lang.c".
    Yes, there's differences; but we deal with them.

    --
    Cheers,
    Robbie Hatley
    lonewolf aatt well dott com
    www dott well dott com slant user slant lonewolf slant
    Robbie Hatley, Mar 8, 2008
    #20
    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. sasquatch

    Makefiles for JAVA

    sasquatch, Nov 8, 2003, in forum: Java
    Replies:
    5
    Views:
    732
    Dale King
    Nov 12, 2003
  2. Harris L

    Ant vs Makefiles

    Harris L, Nov 26, 2004, in forum: Java
    Replies:
    20
    Views:
    10,717
    Jacob
    Dec 2, 2004
  3. dharmesh Gupta

    Making of Makefiles

    dharmesh Gupta, Aug 26, 2003, in forum: C++
    Replies:
    2
    Views:
    332
    Kevin Goodsell
    Aug 26, 2003
  4. Janne Naukkarinen
    Replies:
    1
    Views:
    362
    Jack Klein
    Jan 15, 2004
  5. db
    Replies:
    2
    Views:
    425
Loading...

Share This Page