Thread: Can someone give me a very quick and simple explanation of how to make a makefile

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    32

    Can someone give me a very quick and simple explanation of how to make a makefile

    Since I am getting a lot of help here I thought I would try this question. I always avoid makefiles because the syntax is daunting. What are the benefits of makefiles?

    Every explanation I see is very convoluted, it seems like something that can be easily explained.

    How do I link a few files together into one object.

    Do I only have one main function that calls to other functions in other files? Does it matter which file my main function is in?? Do I have to declare prototype of each function in each different source file?

    And mostly just how do I write the syntax for a makefile.

    Say I had three files.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    A makefile is a database of rules read by the make program that describes how to make your program. Here's an example for 3 .c files where two of them have associated .h files:
    Code:
    prog : prog.o util.o stuff.o
    	gcc -o prog prog.o util.o stuff.o
    
    prog.o : prog.c util.h stuff.h
    	gcc -c prog.c
    
    util.o : util.c util.h
    	gcc -c util.c
    
    stuff.o : stuff.c stuff.h
    	gcc -c stuff.c
    The rule format is
    Code:
    target : prerequisites
    	recipe
    It's IMPORTANT to use a TAB before the recipe lines (that's what marks them as recipe lines, and there can be more than one).

    If you put the above into a file called Makefile (or makefile), then just typing make while in that directory will run make with that makefile. make (when run by just typing make) checks to see if the first target is out of date by comparing its last modification time to that of its prerequisites. If any of the prerequisites are newer than the target, or the target does not exist, then the recipe is executed. The recipe is (usually) designed to re-create the target. If any of the prerequisites is out of date relative to their prerequisites then their recipes will be executed before the main target's recipe.

    It doesn't really matter which file has main defined in it, but the above scenario presumably has main defined in prog.c.

    There are other things, like variables and fake files (clean, for example), implicit rules, etc., but the above is enough to get you started. Read GNU `make' to learn all about it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. give me an explanation
    By shivam99aa in forum C Programming
    Replies: 11
    Last Post: 12-21-2012, 05:57 AM
  2. give me an explanation
    By shivam99aa in forum C Programming
    Replies: 1
    Last Post: 10-24-2011, 09:14 AM
  3. Replies: 2
    Last Post: 04-27-2011, 04:14 PM
  4. how here j is printing 7.give the explanation
    By sunil17 in forum C Programming
    Replies: 4
    Last Post: 09-03-2010, 09:36 AM
  5. Replies: 1
    Last Post: 02-11-2002, 12:00 PM