Makefile doesn't detect header file change

Discussion in 'C Programming' started by lovecreatesbeauty, Mar 20, 2012.

  1. My Makefile doesn't detect the change of header files.

    This little makefile follows sec 2.6 of GNU make manual, and I
    remember it worked before when header files changed.

    Where am i wrong?

    Thanks

    $ touch b.h
    $ make
    make: `a.out' is up to date.
    $

    $ pwd
    /home/ljh/temp/src
    $ ls
    a.c b.c b.h Makefile
    $ cat Makefile
    CC = gcc
    CFLAGS = -ansi -pedantic -Wall -W
    LDFLAGS =
    OBJS = a.o b.o
    OUT = a.out

    $(OUT) : $(OBJS)
    $(CC) $(LDFLAGS) $^ -o $@

    a.c b.c : b.h

    ..PHONY : clean

    clean :
    rm $(OUT) $(OBJS)

    $ make
    gcc -ansi -pedantic -Wall -W -c -o a.o a.c
    a.c: In function ¡®main¡¯:
    a.c:7: warning: implicit declaration of function ¡®b¡¯
    gcc -ansi -pedantic -Wall -W -c -o b.o b.c
    gcc a.o b.o -o a.out
    $ touch b.c
    $ make
    gcc -ansi -pedantic -Wall -W -c -o b.o b.c
    gcc a.o b.o -o a.out
    $ make
    make: `a.out' is up to date.
    $ touch b.h
    $ make
    make: `a.out' is up to date.
    $
    $ make -v
    GNU Make 3.81
    Copyright (C) 2006 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.
    There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
    PARTICULAR PURPOSE.

    This program built for i486-pc-linux-gnu
    $ uname -a
    Linux debian 2.6.32-5-686 #1 SMP Mon Jan 16 16:04:25 UTC 2012 i686 GNU/
    Linux
    $ cat /etc/debian_version
    6.0.4
    $
     
    lovecreatesbeauty, Mar 20, 2012
    #1
    1. Advertising

  2. lovecreatesbeauty

    Ian Collins Guest

    On 03/20/12 03:15 PM, lovecreatesbeauty wrote:
    > My Makefile doesn't detect the change of header files.
    >
    > This little makefile follows sec 2.6 of GNU make manual, and I
    > remember it worked before when header files changed.
    >
    > Where am i wrong?


    Posting here? Try are more appropriate group.

    You probably need the enable dependency checking in your makefile.

    --
    Ian Collins
     
    Ian Collins, Mar 20, 2012
    #2
    1. Advertising

  3. lovecreatesbeauty

    Kaz Kylheku Guest

    On 2012-03-20, lovecreatesbeauty <> wrote:
    > My Makefile doesn't detect the change of header files.
    >
    > a.c b.c : b.h


    Here you are saying that a.c and b.c are objects that are built from b.h.

    But that is not the case, since they are not built; a.c and b.c they are
    primary objects with no dependencies.

    It is b.o and a.o which depend on b.h.

    > $ touch b.h
    > $ make


    Hmm, a better message might be "no rule to make a.c from b.h",
    since you've declared the dependency, and a.c is out of date
    with respect to b.h under that dependency, and there isn't
    any way to make a.c from b.h".
     
    Kaz Kylheku, Mar 20, 2012
    #3
  4. On Mar 20, 10:48 am, Kaz Kylheku <> wrote:
    > On 2012-03-20, lovecreatesbeauty <> wrote:
    >
    > > My Makefile doesn't detect the change of header files.

    >
    > > a.c b.c : b.h

    >
    > Here you are saying that a.c and b.c are objects that are built from b.h.
    >
    > But that is not the case, since they are not built; a.c and b.c they are
    > primary objects with no dependencies.
    >
    > It is b.o and a.o which depend on b.h.
    >


    Thank you very much for pointing this out.

    > > $ touch b.h
    > > $ make

    >
    > Hmm, a better message might be "no rule to make a.c from b.h",
    > since you've declared the dependency, and a.c is out of date
    > with respect to b.h under that dependency, and there isn't
    > any way to make a.c from b.h".


    Does my implicit rule for compiling cause the "no rule.." message, or
    is it an implementation bug:)
     
    lovecreatesbeauty, Mar 20, 2012
    #4
  5. On Mar 20, 10:48 am, Kaz Kylheku <> wrote:
    > On 2012-03-20, lovecreatesbeauty <> wrote:
    >
    > > My Makefile doesn't detect the change of header files.

    >
    > > a.c b.c : b.h

    >
    > Here you are saying that a.c and b.c are objects that are built from b.h.
    >
    > But that is not the case, since they are not built; a.c and b.c they are
    > primary objects with no dependencies.
    >
    > It is b.o and a.o which depend on b.h.
    >


    I add auto-dependencies generation for header files to avoid specify
    individual header for source files.

    I tried use wildcard for the SRCS variable in Makefile, but caused
    some errors.

    $ cat Makefile
    CC = gcc
    CFLAGS = -ansi -pedantic -Wall -W
    CPPFLAGS = -I include
    LDFLAGS =
    SRCS = a.c b.c
    OBJS = $(patsubst %.c,%.o,$(SRCS))
    OUT = a.out

    vpath %.c src
    vpath %.h include

    $(OUT): $(OBJS)
    $(CC) $(LDFLAGS) $^ -o $@

    include $(SRCS:.c=.d)

    %.d: %.c
    @set -e; rm -f $@; \
    $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    rm -f $@.$$$$

    ..PHONY : clean
    clean:
    rm *.d $(OBJS) $(OUT)

    $
    $ pwd
    /home/ljh/temp
    $
    $ ls . ./src/ ./include/
    ..:
    include Makefile src

    ../include/:
    b.h

    ../src/:
    a.c b.c
    $
     
    lovecreatesbeauty, Mar 20, 2012
    #5
  6. lovecreatesbeauty <> writes:
    [snip]
    > I add auto-dependencies generation for header files to avoid specify
    > individual header for source files.
    >
    > I tried use wildcard for the SRCS variable in Makefile, but caused
    > some errors.
    >
    > $ cat Makefile
    > CC = gcc
    > CFLAGS = -ansi -pedantic -Wall -W

    [snip]

    Makefiles aren't specific to C. You're likely to get better help in
    comp.unix.programmer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 20, 2012
    #6
    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. Newsgroup - Ann
    Replies:
    4
    Views:
    746
    lilburne
    Nov 2, 2003
  2. HT-Lab

    makefile and header files

    HT-Lab, Jul 12, 2007, in forum: C++
    Replies:
    8
    Views:
    19,113
    HT-Lab
    Jul 13, 2007
  3. MZ
    Replies:
    7
    Views:
    874
    Ed Mullen
    Mar 17, 2008
  4. mlt
    Replies:
    2
    Views:
    907
    Jean-Marc Bourguet
    Jan 31, 2009
  5. aqura
    Replies:
    7
    Views:
    653
    Victor Bazarov
    Sep 9, 2011
Loading...

Share This Page