writing a makefile?

This is a discussion on writing a makefile? within the C Programming forums, part of the General Programming Boards category; how do I write a makefile for program that has test.c file, and a test.h header file? thanks....

  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,053
    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,185
    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
    22,159
    Looks like wwwnuwan started a new thread, so I am closing this old one.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09:07 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 10:19 AM
  5. Writing makefile in Unix
    By Mangesh in forum C Programming
    Replies: 2
    Last Post: 09-11-2001, 11:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21