Thread: A question about an interesting Makefile

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A question about an interesting Makefile

    I am reading GNU Make's manual, here is an example from it:

    http://www.cs.utah.edu/dept/old/texi..._toc.html#SEC8

    Code:
    objects = main.o kbd.o command.o display.o \
              insert.o search.o files.o utils.o
    
    edit : $(objects)
            cc -o edit $(objects)
    main.o : main.c defs.h
            cc -c main.c
    kbd.o : kbd.c defs.h command.h
            cc -c kbd.c
    command.o : command.c defs.h command.h
            cc -c command.c
    display.o : display.c defs.h buffer.h
            cc -c display.c
    insert.o : insert.c defs.h buffer.h
            cc -c insert.c
    search.o : search.c defs.h buffer.h
            cc -c search.c
    files.o : files.c defs.h buffer.h command.h
            cc -c files.c
    utils.o : utils.c defs.h
            cc -c utils.c
    clean :
            rm edit $(objects)
    It's interesting that main.o depends on defs.h. I don't think it's necessary to put defs.h for main.o . Because if main.o depends on defs.h, you just need to include <defs.h> in main.c, there is no need to put defs.h as a dependent in a Makefile.

    Am I right?

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    I take back my question. Now I understand that claiming:
    main.o : main.c defs.h
    is to let complier know that if defs.h changes, main.o has to be recompiled.
    It has nothing to do with
    g++ -c -o main.o main.c

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by meili100 View Post
    I take back my question. Now I understand that claiming:
    main.o : main.c defs.h
    is to let complier know that if defs.h changes, main.o has to be recompiled.
    It has nothing to do with
    g++ -c -o main.o main.c
    That is exactly it. You should list all files that the object file depends on, so if your main.c include defs.h, then you need to specify that on the dependencies for main.o. All include files that are part of the project should be specified this way. Gcc has the option "-MM" that will generate a dependency list for the input file(s).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile question
    By -EquinoX- in forum C Programming
    Replies: 21
    Last Post: 10-16-2008, 07:39 PM
  2. interesting question about new and nothrow new
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2008, 03:53 AM
  3. Question about binary trees and files
    By satory in forum C Programming
    Replies: 9
    Last Post: 03-06-2006, 06:28 AM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM