Results 1 to 5 of 5

Thread: Using a makefile

Hybrid View

  1. #1
    Join Date
    Nov 2006
    Location
    Pennsylvania
    Beans
    423

    Using a makefile

    I am reading a book on programming in C.

    It is getting into working on larger projects and showed how to make a simple little makefile.

    I have never used a makefile before and the book is a little old.. it is from 1999 so I want to make sure I am doing everything right.

    Code:
    File: makefile.gcc
    
    #-------------------------------------------------#
    #	Makefile for Unix systems					  #
    #	Using a GNU C compiler						  #
    #-------------------------------------------------#
    CC=gcc
    CFLAGS=-g -D__USE_FIXED_PROTOTYPES__ -ansi
    #
    # Compiler flags:
    #		-g	-- Enable debugging
    #		-Wall	-- Turn on all warnings 
    #		-D__USE_FIXED_PROTOTYPES__
    #			-- Force the compiler to use the correct headers
    #		-ansi	-- Don't use GNU extensions. Stick to ANSI C.
    
    7-1: 7-1.c
    	$(CC) $(CFLAGS) -o output 7-1.c
    
    clean:
    	rm -f output
    In terminal I type:
    Code:
    $ make -f makefile.gcc
    gcc -g -D__USE_FIXED_PROTOTYPES__ -ansi -o output 7-1.c
    $
    Is this makefile workable? I compiled the program before making the makefile with gcc so I don't think makefile will do it again.
    Your Ubuntu User number is # 15355

    A must Read for anyone interested in Computer Programming.

  2. #2
    Join Date
    Feb 2007
    Beans
    2,729

    Re: Using a makefile

    It is usable, but not quite correct. You should also change the -o output to -o 7-1, and of course update your clean rule to use the correct name as well.

    You'll want to make some changes when you have more than one source file.

    You can test that it works:

    Code:
    touch 7-1.c
    make -f makefile.gcc
    or even

    Code:
    rm 7-1
    make -f makefile.gcc
    Do yourself a favor - rename makefile.gcc to Makefile, and then all you have to do is type:

    Code:
    make
    MrC
    Last edited by Mr. C.; July 10th, 2007 at 05:21 AM.

  3. #3
    Join Date
    Jan 2005
    Location
    South Africa
    Beans
    2,098
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Using a makefile

    I have posted a universal makefile on http://www.linuxquestions.org/questi...d.php?t=380437 (post #4).
    Only thing it does not do is descend into subdirectories (which would be very usefull for bigger projects).

  4. #4
    Join Date
    Nov 2006
    Location
    Pennsylvania
    Beans
    423

    Re: Using a makefile

    Mr. C.

    Thanks for the tips. Everything is working great now, typing 'make' is a lot easier

    Is there anyway I could make my makefile automatically execute the new executable file after compiling if there are no errors?
    Your Ubuntu User number is # 15355

    A must Read for anyone interested in Computer Programming.

  5. #5
    Join Date
    Feb 2007
    Beans
    2,729

    Re: Using a makefile

    Sure,

    Change:

    Code:
    7-1: 7-1.c
    	$(CC) $(CFLAGS) -o output 7-1.c
    to
    Code:
    7-1: 7-1.c
    	$(CC) $(CFLAGS) -o output 7-1.c
            ./7-1
    Your 7.1 command will run only after the previous CC command completes successfully.

    MrC

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •