Thread: Quick Makefile Question

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    Quick Makefile Question

    Sometimes when I run my makefile that consist of the following

    [code]
    default:
    g++ -o prog1 prog1.cpp

    clean:
    rm -rf *.o
    rm -rf core*
    [code]

    It sometimes says the file is updated, but I've just updated the source and it won't call the default tag for some reason.

    I'm suppose to make a makefile that properly makes the program. Calling "make default" should only do the necessary work - it shouldn't recompile anything that doesn't need it. "make clean" should remove any core dump files and the .o and executable files.

    Is this correct? I think someone said I had to worry about depencies or something, I guess what I have to do is that if the source is update or executable is, then it shouldn't update or something?

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    you need to create a dependancy
    Code:
    prog1: prog1.cpp
    g++ prog1.cpp -o prog1
    ......
    Target prog depends on prog1.cpp, so is prog is older than prog1.cpp, prog1 is built.
    If you want it to be the first command to be run, keep it at the beginning of the makefile.
    More info:
    http://www.gnu.org/software/make/man...mono/make.html
    Last edited by xErath; 05-16-2005 at 01:25 PM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    So if prog.o is older than prog.c then it will compile the source with this makefile, else it will skip it ?

    Code:
    #Makefile
    #############
    
    default: clean compile
    
    compile: prog.cpp
                g++ -o prog prog.cpp
    
    clean:
                rm -f *.o prog a.out core*

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by cisokay
    So if prog.o is older than prog.c then it will compile the source with this makefile, else it will skip it ?

    Code:
    #Makefile
    #############
    
    default: clean compile
    
    compile: prog.cpp
                g++ -o prog prog.cpp
    
    clean:
                rm -f *.o prog a.out core*
    A simple make statement is like this:
    Code:
    target: dependancies
        command
    if any of the dependancies is newer than target, command is run. Note that the command must have a tab char before.

    Your make will always compile prog. You're stating that compile depends on prog.cpp. When that command is run no file named compile exists, so the command is always run, whenever make is run.
    Also you're not stating that prog depends on prog.cpp. re-read my previous post.

    and finally since I seriously doubt that there'll ever be any file called compile declare compile like this
    .PHONY compile
    In other words, compile will always be a word passed through the command line like clean
    To sum up we have
    Code:
    prog: prog.cpp
        g++ prog.cpp -o prog
    
    .PHONY clean compile
    
    compile: prog
    
    clean:
        rm -f *.o prog a.out core*

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Thanks, I think I get it now. One question, I get makefile:4: *** missing separator. Stop. error. I'm copying directly from your post.

    It isn't liking the
    .PHONY clean compile

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Put a colon after the .PHONY:

    .PHONY: clean compile
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    it happens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 03:54 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM