Need help with Makefile

This is a discussion on Need help with Makefile within the C++ Programming forums, part of the General Programming Boards category; For some reason when I try to use a makefile with a program of mine, it won't compile. However, when ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Question Need help with Makefile

    For some reason when I try to use a makefile with a program of mine, it won't compile. However, when I compile it from the command line it works fine...

    Here is the code for my makefile

    Code:
    # Makefile for Stack program
    
    all: 	main	
    
    main:	stdlib.h iostream.h Stack.h string.h
    	         g++ Stack.cpp calc.cpp  -o calc
    now the Stack.h header file is something that I wrote... could that be related to why the makefile won't work?

    Does anyone have any resources as to how to make a makefile with your own source code? That'd be excellent. Thanks.

  2. #2
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    What's the compiler error? Or does make complain. If so, did you use a "real" tab under main: and before g++?

    It's also always a good idea to name the target after the file it generates. Another issue is, that the calc target obviously depends on Stack.cpp, so you should add it to calc's dependencies and remove non-local dependencies.

    Code:
       all: calc    
       
       calc: Stack.cpp Stack.h calc.cpp
       <tab>g++ Stack.cpp calc.cpp  -o calc
    Last edited by Nyda; 09-27-2004 at 10:18 AM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Post

    I get undefined reference errors dealing with the Stack class that I have.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Unhappy

    Does this look any better. It still doesn't work...
    Code:
    # Makefile for Stack program
    
    all:	calc
    
    calc:	Stack.o calc.o 
    	g++ stack.o calc.o 
    
    Stack.o: Stack.cpp Stack.h
    	g++ Stack.cpp
    
    calc.o:	calc.cpp Stack.h
    	g++ calc.cpp Stack.cpp
    
    main.o:	calc.cpp Stack.h
    	g++ calc.cpp Stack.cpp -o Calc
    I still keep getting undefined reference errors

    Something strange I notice, right before the undefined reference error list, there is this:
    "g++ Calc.cpp -o Calc"

    It should say:
    "g++ Stack.cpp Calc.cpp -o Calc"
    right?
    Last edited by xshapirox; 09-27-2004 at 10:49 AM. Reason: Mistyped code

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by xshapirox
    I get undefined reference errors dealing with the Stack class that I have.
    Maybe it's not a Makefile error.

    What do you get when you enter the following at the command line?

    g++ Stack.cpp calc.cpp -o calc

    Regards,

    Dave

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by xshapirox
    Does this look any better. It still doesn't work...
    Code:
    # Makefile for Stack program
    
    all:	calc
    
    calc:	Stack.o calc.o 
    	g++ stack.o calc.o 
    
    Stack.o: Stack.cpp Stack.h
    	g++ Stack.cpp
    
    calc.o:	calc.cpp Stack.h
    	g++ calc.cpp Stack.cpp
    
    main.o:	calc.cpp Stack.h
    	g++ calc.cpp Stack.cpp -o Calc
    I still keep getting undefined reference errors

    Something strange I notice, right before the undefined reference error list, there is this:
    "g++ Calc.cpp -o Calc"

    It should say:
    "g++ Stack.cpp Calc.cpp -o Calc"
    right?

    Stack.o: Stack.cpp Stack.h
    <tab>g++ Stack.cpp

    Should be something like

    Stack.o: Stack.cpp Stack.h
    <tab>g++ -c Stack.cpp -o Stack.o
    Last edited by Dave Evans; 09-27-2004 at 10:59 AM.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Quote Originally Posted by Dave Evans
    Maybe it's not a Makefile error.

    What do you get when you enter the following at the command line?

    g++ Stack.cpp calc.cpp -o calc

    Regards,

    Dave
    Nah dude, works beautifully when I do that...

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Question

    Could it be how I'm "making" my make file?

    from the prompt I type:
    Code:
    make makefile
    also, becuase I'm trying desperately to get this done asap, I try doing this too

    Code:
    make Calc
    that's when I get my undefined reference errors.

    Thanks for all the help so far.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Also, apparently I found out I'm supposed to just be able to type "make" and it will make my make file; however, this does not happen. Instead I get: "make: *** No targets specified and no makefile found. Stop."

  10. #10
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    http://www.linuxgazette.com/issue83/heriyanto.html

    gives info on creating make files
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by xshapirox
    Could it be how I'm "making" my make file?

    from the prompt I type:
    Code:
    make makefile
    also, becuase I'm trying desperately to get this done asap, I try doing this too

    Code:
    make Calc
    that's when I get my undefined reference errors.

    Thanks for all the help so far.
    Do you have a file named "Makefile" that has "makefile" as a target? What do you get when yor enter "make makefile"

    Does your makefile have "Calc" as a target? So far I have seen "calc", not Calc.

    The first thing you posted was close to a minimal makefile. Maybe you can try this

    (note there is a real tab before the g++, just as in your original)

    Code:
    # Makefile for Stack program
    	
    
    calc: Stack.h Stack.cpp calc.cpp
    	g++ Stack.cpp calc.cpp  -o calc

    [additional action]
    Name this file "makefile".
    Before running make on this, delete file "calc" if it exists in your directory, or simply touch Stack.cpp, calc.cpp and/or Stack.h
    [/additional action]

    Regards,

    Dave
    Last edited by Dave Evans; 09-27-2004 at 11:30 AM.

  12. #12
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Here ya go... If you copy and paste, make sure the tabs are still tabs.

    Code:
    EXEC = Calc
    OBJS = Stack.o calc.o
    CC = g++
    
    all: $(OBJS)
    	$(CC) $(OBJS) -o $(EXEC)
    
    run: all
    	./$(EXEC)
    
    clean:
    	rm -f $(OBJS) $(EXEC) core

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Perspective
    Here ya go... If you copy and paste, make sure the tabs are still tabs.

    Code:
    EXEC = Calc
    OBJS = Stack.o calc.o
    CC = g++
    
    all: $(OBJS)
    	$(CC) $(OBJS) -o $(EXEC)
    
    run: all
    	./$(EXEC)
    
    clean:
    	rm -f $(OBJS) $(EXEC) core
    Dependency for Stack.h?

  14. #14
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Quote Originally Posted by xshapirox
    Does this look any better. It still doesn't work...
    Code:
    # Makefile for Stack program
    
    all:	calc
    
    calc:	Stack.o calc.o 
    	g++ stack.o calc.o 
    
    Stack.o: Stack.cpp Stack.h
    	g++ Stack.cpp
    
    calc.o:	calc.cpp Stack.h
    	g++ calc.cpp Stack.cpp
    
    main.o:	calc.cpp Stack.h
    	g++ calc.cpp Stack.cpp -o Calc
    I still keep getting undefined reference errors

    Something strange I notice, right before the undefined reference error list, there is this:
    "g++ Calc.cpp -o Calc"

    It should say:
    "g++ Stack.cpp Calc.cpp -o Calc"
    right?

    There seems to be a couple problems. Specificaly with lines
    Code:
    all:	calc
    
    calc:	Stack.o calc.o 
    	g++ stack.o calc.o
    which have tabs in the dependency headers. Also the label 'calc' doesnt result in the calc executable you want. With the rest of the makefile it seems as though you have an extra label 'main.o' which seems where you attempt to make the real executable but it will never get hit because 'main.o' is never a dependecy of anything. Nor is that the name of the resulting executable so the 'main.o' labal would always run if it could be reached.

    The makefile should look something like this:
    Code:
    # This file should be called makefile
    # Makefile for Stack program
    # when 'make' gets run it hits the first label then procedes otherwise
    # use 'make label' to target a specific label bypassing some dependencies  
    
    all: calc
    
    calc: Stack.o calc.o
    	g++ calc.o Stack.o -o calc
    
    Stack.o: Stack.cpp Stack.h
    	g++ -c Stack.cpp
    
    calc.o:	calc.cpp Stack.h
    	g++ -c calc.cpp
    
    clean:
    	rm *.o
    	rm calc.exe
    If calc.cpp is where main is then this should work. The label 'all' could be dropped as 'make' with no targets hits the first label it gets to. Some people always include 'all' for automake stuff because they often generically always target 'all'. The label 'calc' has dependencies on Stack.o and calc.o. Since calc.cpp likely #includes Stack.h and uses the stack object, we have Stack.o first. Once both dependencies have been checked it finally finishes with 'calc'.

    It's important to make labels the same as the resulting object or exe file. There must be absolutely no tabs in the 'label: dependencies' line. Make sure all dependencies have valid labels.

    I believe if your still having trouble this should help you.

  15. #15
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Dave Evans
    Dependency for Stack.h?
    in small projects im too lazy to define .o build targets for all object files, especially if theres only one header dependancy. So, If my header changes its, "make clean" " make all"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile Problem: None rule to make target
    By chris24300 in forum Linux Programming
    Replies: 25
    Last Post: 06-17-2009, 10:45 AM
  2. Building a project using a Makefile
    By starcatcher in forum Windows Programming
    Replies: 2
    Last Post: 11-23-2008, 11:50 PM
  3. unix makefile won't work but works in Dev C++
    By jk1998 in forum C++ Programming
    Replies: 1
    Last Post: 06-09-2007, 04:54 PM
  4. makefile blues....
    By WaterNut in forum C Programming
    Replies: 6
    Last Post: 05-30-2005, 09:22 PM
  5. makefile
    By twans in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 AM

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