Thread: writing a makefile?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    20

    writing a makefile?

    how do I write a makefile for program that has test.c file, and a test.h header file?

    thanks.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You'll need a tutorial for makefiles. I'm sure you can find one on the net, but I don't know any good sites. However the general process is as follows:

    You have a make target for your program, and a make target for each of the .o files, which correspond to each c file. In this case that means 2 make targets. The program itself must depend on all the c files. The .o file must depend on the .c file it corresponds to and all it's included files.

    To specify a make target, provide the name of the target, fallowed by a colon, followed by each of its dependencies. Indent the next line with a tab (not spaces!) and print the command needed to make that target. If multiple commands are needed, use multiple indented lines. Consult your compiler documentation for the commands to build the respective make targets.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    One other thing to note is that make generally understands suffixes. So for instance, technically my makefiles should have targets for newton.o (et cetera) but they don't; make can see newton.c (actually newton.f, but never mind) and knows how to turn .c into .o, so it just does it. (This probably makes me a bad person.) You can also set up a rule for *.o based on *.c. You can find the manual for gnu make at gnu.org. (Of course, if you're not using gnu make, the above is probably right but there are going to be little differences.)

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Here's what I used to learn about makefiles:
    http://www.gnu.org/software/make/manual/make.html

    Although I prefer the PDF version since you can print it out and read it on the bus, or keep it beside your bed in case you can't sleep...
    http://www.gnu.org/software/make/manual/make.pdf

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    quick/dirty GNU Makefile:
    Code:
    CFLAGS=-W -Wall
    
    all: test.exe
    
    test.o: test.c test.h
        gcc $(CFLAGS) -c test.c
    
    test.exe: test.o
        gcc -o test.exe test.o
    
    clean:
        rm -f test.o test.exe
    NOTE: There MUST be tabs in Makefile, and i couldn't input them here so don't just copy/paste, replace spaces with tabs !!!

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    22
    Hi, Is there any tools to create makemake file.
    I hv set of source files and a header file in my program. i need to create a make file for this.
    I went through the google to find how to and im still in that phase.
    but at the middle of it i found a tool called automake.
    What make the difference using a tool to create a make file or writing its by my own.

    As a beginer which one would you recommend me?
    thanks.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This is a new thread, so if some moderator can help by splitting this and the previous post off, it would help.

    Some compilers are able to produce a dependency listing, which is part of generating a makefile. There are tools that go well beyond that - most IDE's have a way that produce a makefile, but it's often just as easy to write the bit that sets up the overall project yourself (because even in the IDE, you have to select which source files get compiled, and place them into some box or such).

    It is not hard if you understand a bit of makefile structure. If you don't, then you probably should study a tutorial of makefile structure.

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

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Looks like wwwnuwan started a new thread, so I am closing this old one.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Building a project using a Makefile
    By starcatcher in forum Windows Programming
    Replies: 2
    Last Post: 11-23-2008, 11:50 PM
  2. A simple question about writing a Makefile
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2008, 08:07 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  5. Writing makefile in Unix
    By Mangesh in forum C Programming
    Replies: 2
    Last Post: 09-11-2001, 10:48 PM