Tell me about makefiles

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

    Tell me about makefiles


    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?
  • Eric Sosman

    #2
    Re: Tell me about makefiles

    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.progr ammer than here.

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • Richard Heathfield

      #3
      Re: Tell me about makefiles

      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

      Comment

      • Richard Heathfield

        #4
        Re: Tell me about makefiles

        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.progr ammer 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

        Comment

        • santosh

          #5
          Re: Tell me about makefiles

          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?

          Comment

          • Ian Collins

            #6
            Re: Tell me about makefiles

            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.

            Comment

            • Harald van =?UTF-8?b?RMSzaw==?=

              #7
              Re: Tell me about makefiles

              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.

              Comment

              • santosh

                #8
                Re: Tell me about makefiles

                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
                >>preprocesso r 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.

                Comment

                • Antoninus Twink

                  #9
                  Re: Tell me about makefiles

                  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.

                  Comment

                  • Robbie Hatley

                    #10
                    Re: Tell me about makefiles


                    "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


                    Comment

                    • Richard Harter

                      #11
                      Re: Tell me about makefiles

                      On Fri, 7 Mar 2008 23:46:34 +0100 (CET), Antoninus Twink
                      <nospam@nospam. invalidwrote:
                      >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, cri@tiac.net
                      http://home.tiac.net/~cri, http://www.varinoma.com
                      Save the Earth now!!
                      It's the only planet with chocolate.

                      Comment

                      • Antoninus Twink

                        #12
                        Re: Tell me about makefiles

                        On 7 Mar 2008 at 23:29, Richard Harter wrote:
                        On Fri, 7 Mar 2008 23:46:34 +0100 (CET), Antoninus Twink
                        ><nospam@nospam .invalidwrote:
                        >
                        >>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.

                        Comment

                        • Richard Tobin

                          #13
                          Re: Tell me about makefiles

                          In article <slrnft3kjd.g85 .nospam@nospam. invalid>,
                          Antoninus Twink <nospam@nospam. invalidwrote:
                          >>>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

                          Comment

                          • Default User

                            #14
                            Re: Tell me about makefiles

                            Richard Harter wrote:
                            On Fri, 7 Mar 2008 23:46:34 +0100 (CET), Antoninus Twink
                            <nospam@nospam. invalidwrote:
                            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

                            Comment

                            • CBFalconer

                              #15
                              Re: Tell me about makefiles

                              Richard Harter wrote:
                              Antoninus Twink <nospam@nospam. invalidwrote:
                              >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

                              Comment

                              Working...