headerfiles and make? - Unix

This is a discussion on headerfiles and make? - Unix ; I have a sourcefile main.cpp and a headerfile types.h which I include in main.cpp. In the headerfile I define some types used in main.cpp. I have created a makefile containing: all: main main: main.cpp g++ main.cpp -o main when I ...

+ Reply to Thread
Results 1 to 16 of 16

Thread: headerfiles and make?

  1. headerfiles and make?

    I have a sourcefile main.cpp and a headerfile types.h which I include in
    main.cpp. In the headerfile I define some types used in main.cpp. I have
    created a makefile containing:

    all: main

    main: main.cpp
    g++ main.cpp -o main


    when I type make it compiles fine eventhough I don't write any dependency on
    the types.h. How can I compile using the above makefile when types.h is not
    mentioned?



  2. Re: headerfiles and make?

    On Aug 11, 3:08*am, "saneman" wrote:
    > I have a sourcefile main.cpp and a headerfile types.h which I include in
    > main.cpp. In the headerfile I define some types used in main.cpp. I have
    > created a makefile containing:
    >
    > all: main
    >
    > main: main.cpp
    > *g++ main.cpp -o main
    >
    > when I type make it compiles fine eventhough I don't write any dependencyon
    > the types.h. How can I compile using the above makefile when types.h is not
    > mentioned?


    I'm not sure I understand your question. Is your question "my makefile
    is broken, why does it work?" If so, the answer is that it does not
    work. Do a 'make', then modify 'types.h'. Then do a 'make' again. You
    will see taht the file is not compiled again even though it should be.

    DS

  3. Re: headerfiles and make?

    On Aug 11, 6:47*am, David Schwartz wrote:
    > On Aug 11, 3:08*am, "saneman" wrote:
    >
    > > I have a sourcefile main.cpp and a headerfile types.h which I include in
    > > main.cpp. In the headerfile I define some types used in main.cpp. I have
    > > created a makefile containing:

    >
    > > all: main

    >
    > > main: main.cpp
    > > *g++ main.cpp -o main

    >
    > > when I type make it compiles fine eventhough I don't write any dependency on
    > > the types.h. How can I compile using the above makefile when types.h isnot
    > > mentioned?

    >
    > I'm not sure I understand your question. Is your question "my makefile
    > is broken, why does it work?" If so, the answer is that it does not
    > work. Do a 'make', then modify 'types.h'. Then do a 'make' again. You
    > will see taht the file is not compiled again even though it should be.
    >
    > DS


    No I think you are referring to how the compiler knows to include your
    header file even though you didn't specify it in your makefile? I'm
    definately no expert on this but I think the #include directive you
    used in your source code tells the compiler to look for it. It looks
    in the current directory for it "." if no path is specified.

  4. Re: headerfiles and make?


    "David Schwartz" skrev i en meddelelse
    news:a91cde78-29b5-4850-8ed1-d8cdce9a2892@u6g2000prc.googlegroups.com...
    On Aug 11, 3:08 am, "saneman" wrote:
    > I have a sourcefile main.cpp and a headerfile types.h which I include in
    > main.cpp. In the headerfile I define some types used in main.cpp. I have
    > created a makefile containing:
    >
    > all: main
    >
    > main: main.cpp
    > g++ main.cpp -o main
    >
    > when I type make it compiles fine eventhough I don't write any dependency
    > on
    > the types.h. How can I compile using the above makefile when types.h is
    > not
    > mentioned?


    I'm not sure I understand your question. Is your question "my makefile
    is broken, why does it work?" If so, the answer is that it does not
    work. Do a 'make', then modify 'types.h'. Then do a 'make' again. You
    will see taht the file is not compiled again even though it should be.

    DS

    I have tried to run make which compiles the main.cpp. I have then tried to
    change :

    #include

    to

    #iiiiinclude

    and then run make again. This time I get an error so the file types.h is
    compiled again eventhough the makefile knows nothing about types.h



  5. Re: headerfiles and make?


    "darren" skrev i en meddelelse
    news:df5fd6b5-953c-459e-809d-472b1930947f@v1g2000pra.googlegroups.com...
    On Aug 11, 6:47 am, David Schwartz wrote:
    > On Aug 11, 3:08 am, "saneman" wrote:
    >
    > > I have a sourcefile main.cpp and a headerfile types.h which I include in
    > > main.cpp. In the headerfile I define some types used in main.cpp. I have
    > > created a makefile containing:

    >
    > > all: main

    >
    > > main: main.cpp
    > > g++ main.cpp -o main

    >
    > > when I type make it compiles fine eventhough I don't write any
    > > dependency on
    > > the types.h. How can I compile using the above makefile when types.h is
    > > not
    > > mentioned?

    >
    > I'm not sure I understand your question. Is your question "my makefile
    > is broken, why does it work?" If so, the answer is that it does not
    > work. Do a 'make', then modify 'types.h'. Then do a 'make' again. You
    > will see taht the file is not compiled again even though it should be.
    >
    > DS


    No I think you are referring to how the compiler knows to include your
    header file even though you didn't specify it in your makefile? I'm
    definately no expert on this but I think the #include directive you
    used in your source code tells the compiler to look for it. It looks
    in the current directory for it "." if no path is specified.

    Yes this seems to be the case. I have always thought that headerfiles should
    be supplied in a makefile, but it seems that this is not necessary.



  6. Re: headerfiles and make?

    saneman wrote:
    > "darren" skrev i en meddelelse
    > news:df5fd6b5-953c-459e-809d-472b1930947f@v1g2000pra.googlegroups.com...
    > On Aug 11, 6:47 am, David Schwartz wrote:
    > > On Aug 11, 3:08 am, "saneman" wrote:
    > >
    > > > I have a sourcefile main.cpp and a headerfile types.h which I include in
    > > > main.cpp. In the headerfile I define some types used in main.cpp. I have
    > > > created a makefile containing:

    > >
    > > > all: main

    > >
    > > > main: main.cpp
    > > > g++ main.cpp -o main

    > >
    > > > when I type make it compiles fine eventhough I don't write any
    > > > dependency on
    > > > the types.h. How can I compile using the above makefile when types.h is
    > > > not
    > > > mentioned?

    > >
    > > I'm not sure I understand your question. Is your question "my makefile
    > > is broken, why does it work?" If so, the answer is that it does not
    > > work. Do a 'make', then modify 'types.h'. Then do a 'make' again. You
    > > will see taht the file is not compiled again even though it should be.


    > No I think you are referring to how the compiler knows to include your
    > header file even though you didn't specify it in your makefile? I'm
    > definately no expert on this but I think the #include directive you
    > used in your source code tells the compiler to look for it. It looks
    > in the current directory for it "." if no path is specified.


    > Yes this seems to be the case. I have always thought that headerfiles should
    > be supplied in a makefile, but it seems that this is not necessary.


    Since a Makefile isn't necessary for compiling a program the header
    file must be found by the compiler on its own. The '#include' di-
    rectlive tells the compiler to look for a header file and evaluate
    its content immediately. Makefiles have nothing to do with it. It
    works whether you use a Makefile or not.

    You should put the header files into the dependencies in a Makefile
    to ensure that files that include the header file get recompiled if
    the header file has been changed. Make doesn't look into the source
    files all by itself to check which header files get included, so it
    has no idea if a certain source file includes a header file and that
    this source file then should be recompiled when the header file got
    changed. It's your resonsibility to tell make that there's this de-
    pendency.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  7. Re: headerfiles and make?

    saneman wrote:

    > "David Schwartz" skrev i en meddelelse
    > news:a91cde78-29b5-4850-8ed1-d8cdce9a2892@u6g2000prc.googlegroups.com...
    > On Aug 11, 3:08 am, "saneman" wrote:
    > > I have a sourcefile main.cpp and a headerfile types.h which I include in
    > > main.cpp. In the headerfile I define some types used in main.cpp. I have
    > > created a makefile containing:
    > >
    > > all: main
    > >
    > > main: main.cpp
    > > g++ main.cpp -o main
    > >
    > > when I type make it compiles fine eventhough I don't write any dependency
    > > on
    > > the types.h. How can I compile using the above makefile when types.h is
    > > not
    > > mentioned?


    > I'm not sure I understand your question. Is your question "my makefile
    > is broken, why does it work?" If so, the answer is that it does not
    > work. Do a 'make', then modify 'types.h'. Then do a 'make' again. You
    > will see taht the file is not compiled again even though it should be.


    > I have tried to run make which compiles the main.cpp. I have then tried to
    > change :


    > #include


    > to


    > #iiiiinclude


    > and then run make again. This time I get an error so the file types.h is
    > compiled again eventhough the makefile knows nothing about types.h


    The error you get for this has nothing to do with types.h (which also
    doesn't get compiled but included during compilation). The error you
    get is about a compiler directlve ('#iiiiinclude') that doesn't
    exist.

    If you want to make sure that make know that main.cpp needs to be
    recompiled when types.h got changed simply change the Makefile to

    main: main.cpp types.h
    g++ main.cpp -o main

    This tells make that the file main must be re-made if main.cpp and/or
    types.h are newer than main.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  8. Re: headerfiles and make?

    In <48a09242$0$90274$14726298@news.sunsite.dk> "saneman" writes:

    > I have always thought that headerfiles should be supplied in a makefile,
    > but it seems that this is not necessary.


    If you edit types.h, your makefile won't recompile the program because
    you didn't tell make that the program depends on types.h.

    --
    John Gordon A is for Amy, who fell down the stairs
    gordon@panix.com B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"


  9. Re: headerfiles and make?

    On Aug 11, 12:24*pm, "saneman" wrote:

    > I have tried to run make which *compiles the main.cpp. I have then tried to
    > change :
    >
    > #include
    >
    > to
    >
    > #iiiiinclude
    >
    > and then run make again. This time I get an error so the file types.h is
    > compiled again eventhough the makefile knows nothing about types.h


    The file 'types.h' was compiled again because it's included by
    'main.cpp'. Every time 'main.cpp' is compiled, every file it includes
    will also be compiled. This has nothing whatsoever to do with the
    makefile but the nature of C compilation.

    DS

  10. Re: headerfiles and make?


    "John Gordon" skrev i en meddelelse
    news:g7q6m9$rmh$1@reader1.panix.com...
    > In <48a09242$0$90274$14726298@news.sunsite.dk> "saneman"
    > writes:
    >
    >> I have always thought that headerfiles should be supplied in a makefile,
    >> but it seems that this is not necessary.

    >
    > If you edit types.h, your makefile won't recompile the program because
    > you didn't tell make that the program depends on types.h.
    >



    As I wrote I did edit the types.h file and got an error eventhough it was
    not typed in the makefile.



  11. Re: headerfiles and make?

    On Aug 11, 3:54*pm, "saneman" wrote:

    > As I wrote I did edit the types.h file and got an error eventhough it was
    > not typed in the makefile.


    You got an error compiling a C file that includes types.h -- the
    makefile didn't have anything to do with anything.

    DS

  12. Re: headerfiles and make?


    "David Schwartz" skrev i en meddelelse
    news:8211b42f-f201-4e59-8017-169438025d5e@p31g2000prf.googlegroups.com...
    On Aug 11, 3:54 pm, "saneman" wrote:

    > As I wrote I did edit the types.h file and got an error eventhough it was
    > not typed in the makefile.


    You got an error compiling a C file that includes types.h -- the
    makefile didn't have anything to do with anything.

    DS


    I know that , but my point is that there is no reason to make headerfile
    dependencies in a makefile since g++ or gcc will always give a message it an
    error is typed in a headerfile.



  13. Re: headerfiles and make?

    "saneman" writes:
    > "David Schwartz" skrev i en meddelelse
    > news:8211b42f-f201-4e59-8017-169438025d5e@p31g2000prf.googlegroups.com...
    > On Aug 11, 3:54 pm, "saneman" wrote:
    >
    >>> As I wrote I did edit the types.h file and got an error eventhough
    >>> it was not typed in the makefile.

    >
    >> You got an error compiling a C file that includes types.h -- the
    >> makefile didn't have anything to do with anything.

    >
    > I know that , but my point is that there is no reason to make
    > headerfile dependencies in a makefile since g++ or gcc will always
    > give a message it an error is typed in a headerfile.


    The purpose of a header is to be included into some 'translation
    unit'. The preprocessor will create a combined file from a .c-file and
    all .h-files mentioned in include directives contained in this .c-file
    (and all include directives in these files and so forth). This
    combined file is then processed by the compiler proper. Because the
    compiler processes the contents of all include files, it will find all
    errors contained in them.

    But make will not recompile a .c-file after a header change if there
    is no dependency listing the header.


  14. Re: headerfiles and make?

    >I know that , but my point is that there is no reason to make headerfile
    >dependencies in a makefile since g++ or gcc will always give a message it an
    >error is typed in a headerfile.


    There is a reason to make headerfile dependencies in a makefile since
    fixing incorrect code (which is not so incorrect it doesn't compile; it's
    just wrong) will not cause compilations to be re-done.

    Example:

    #define PI 9.141592 /* should be 3.141592 */

    gcc will *NOT* give a message about this.



  15. Re: headerfiles and make?

    On Aug 11, 11:08*am, "saneman" wrote:
    > I have a sourcefile main.cpp and a headerfile types.h which I include in
    > main.cpp. In the headerfile I define some types used in main.cpp. I have
    > created a makefile containing:
    >
    > all: main
    >
    > main: main.cpp
    > *g++ main.cpp -o main
    >
    > when I type make it compiles fine eventhough I don't write any dependencyon
    > the types.h. How can I compile using the above makefile when types.h is not
    > mentioned?


    make performs two tasks.

    The first task is to decide if any of the targets that you have
    specified new to be re-made because the target is older than some of
    the files that you have defined it depends on

    The second tas is to actually remake the target, using the command
    that you have specified.

    Make is not limited to compilation it could use any series of commands
    to 'make a target' e.g.

    fred.txt : bill.txt
    cp bill.txt fred.txt

    In the above example, fred.txt is the target. It is dependant on
    bill.txt. If bill.txt is newer than fred.txt, then bill.txt is copied
    to fred.txt

    you have to make sure that you have the correct dependency list e.g.

    main : main.cpp
    g++ -o main main.cpp main1.cpp

    In the above example main is actually dependant on main.cpp and
    main1.cpp, because they are both used to buildmain. However the
    dependancy list only specifies main.cpp If only main1.cpp is modified,
    main will not get rebuilt.

    Include directives in source files tell the preprocessor to include
    the text contained in the named file into the current file before it
    is passed to the compiler. Although it is normally header files (with
    a .h) suffix that are included, any file with any suffiix may be
    included

    hence the following is totally valid

    main.txt

    main()
    {
    // put you code here
    }

    main.cpp

    #include


    The command required to build main is

    g++ -o main main.cpp

    but the makfile required to ensure that main is rebuilt if either
    main.txt or main.cpp are changed is

    main : main.cpp main.txt
    g++ -o main main.cpp

  16. Re: headerfiles and make?

    On Aug 12, 2:21*am, "saneman" wrote:

    > I know that , but my point is that there is no reason to make headerfile
    > dependencies in a makefile since g++ or gcc will always give a message itan
    > error is typed in a headerfile.


    You could not be more wrong.

    Consider the following situation:

    1) You have two C files, 'foo.c' and 'bar.c'. They both include
    'types.h'.

    2) The makefile knows that 'foo.o' depends on 'foo.c' and that 'bar.o'
    depends on 'bar.c', but does not know about the 'types.h' dependency.

    3) You do 'make'. All files are now up-to-date.

    4) You update your libc library to a new, incompatible version. You
    also modify 'foo.c'.

    5) You type 'make'. The makefile sees that 'foo.c' was changed and
    compiles it to 'foo.o'.

    6) Sadly, it has no idea that 'bar.o' depends on 'types.h', so it does
    not recompile it.

    7) You link 'foo.o', correctly compiled for this libc, to 'bar.o',
    incorrectly compiled for the old libc, to the *new* libc.

    8) Your executable can now fail in strange and unpredictable ways.

    This is just one example. There are many other possible horrible ways
    makefiles can fail if you don't get the dependencies exactly right. It
    is also way many company's build policies insist on clean builds for
    all release executables (just in case).

    DS

+ Reply to Thread