Thread: G++ Makefile Help Needed

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    G++ Makefile Help Needed

    Hello everyone, I've been having some issues with makefiles. My school uses a Linux server and such, so I have to reference some files in a directory on the network, as well as my own files. Anyway, here is my makefile, comments removed:

    Code:
    FREYS_DIR = /afs/umbc.edu/users/f/r/frey/pub/CMSC341/Proj4
    
    PROJ = Proj4
    CC = /usr/local/bin/g++
    CCLFAGS = -g -ansi -Wall -I .
    
    SOURCES = \
    Proj4.cpp \
    kary_heap.h
    
    PROVIDED_SOURCES = $(FREYS_DIR)/CPUTimer.cpp
    
    OBJECTS = Proj4.o CPUTimer.o
    
    $(PROJ): $(FREYS_DIR)/$(PROJ) $(OBJECTS)
    $(CC) $(CCFLAGS) -o $(PROJ)  $(OBJECTS)
    
    Proj4.o: $(FREYS_DIR)/Proj4.cpp kary_heap.h CPUTimer.cpp CPUTimer.h
    $(CC) $(CCFLAGS) -c $(FREYS_DIR)/Proj4.cpp
    
    CPUTimer.o: CPUTimer.cpp CPUTimer.h
    $(CC) $(CCFLAGS) -c CPUTimer.cpp
    Also, the tabs for the compiling parts have been removed, simply to make it look nice, but they are there.

    Anyway, the error I get is as follows:

    Code:
    make: *** No rule to make target `/afs/umbc.edu/users/f/r/frey/pub/CMSC341/Proj4/Proj4.cpp',
    needed by `/afs/umbc.edu/users/f/r/frey/pub/CMSC341/Proj4/Proj4'.  Stop.
    Can anyone see what I'm doing wrong? I must admit, I'm not too experienced with Makefiles and I have yet to receive a response from the faculty.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by OWD2k7 View Post
    Code:
    make: *** No rule to make target `/afs/umbc.edu/users/f/r/frey/pub/CMSC341/Proj4/Proj4.cpp',
    needed by `/afs/umbc.edu/users/f/r/frey/pub/CMSC341/Proj4/Proj4'.  Stop.
    It means that there is no such file /afs/umbc.edu/users/f/r/frey/pub/CMSC341/Proj4/Proj4.cpp

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    Hmm. Thank you for the reply. I assume this means I need to use the directory where my file is submitted? I'm not entirely sure what part of the Makefile controls this, but I'll tinker around with it some.

    Thanks again!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by OWD2k7 View Post
    Hmm. Thank you for the reply. I assume this means I need to use the directory where my file is submitted? I'm not entirely sure what part of the Makefile controls this, but I'll tinker around with it some.

    Thanks again!
    Well, are your project files in the same directory as the makefile? This makefile is overly complicated.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    Indeed, I thought the Makefile was overly complicated. I typically make mine a lot smaller, but the problem is that I had two make two files (Proj4 and kary_heap), which are in my directory with the Makefile, and reference two other files (CPUTimer.cpp and CPUTimer.h, and thus, CPUTimer.o), so I used an older makefile provided by the teacher which did something similar.

    Tinkering around, I've actually been able to make it work by eliminating all references to to the teacher's directory before Proj4.cpp. The problem now, however, is that it doesn't seem to be getting the teacher's CPUTimer file.

    The modified parts of the makefile:

    Code:
    Proj4.o: Proj4.cpp kary_heap.h CPUTimer.cpp CPUTimer.h
    $(CC) $(CCFLAGS) -c Proj4.cpp
    
    CPUTimer.o: $(FREYS_DIR)/CPUTimer.cpp CPUTimer.h
    $(CC) $(CCFLAGS) -c CPUTimer.cpp
    Which now gives me the output:

    Code:
    make: Circular Proj4 <- Proj4 dependency dropped.
    /usr/local/bin/g++ -g -ansi -Wall -I . -c Proj4.cpp
    Proj4.cpp: In function 'int main(int, char**)':
    Proj4.cpp:17: error: 'CPUTimer' was not declared in this scope
    The make seems to work, but it's ignoring the author's files. Do you have any suggestions? Thanks again for your help.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Proj4.o: Proj4.cpp kary_heap.h CPUTimer.cpp CPUTimer.h
    Get rid of that, you don't need one .cpp depending on another. You already have the header file, that's all you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    Quote Originally Posted by Salem View Post
    > Proj4.o: Proj4.cpp kary_heap.h CPUTimer.cpp CPUTimer.h
    Get rid of that, you don't need one .cpp depending on another. You already have the header file, that's all you need.
    Should I actually change that to CPUTimer.o?

    Having done some more modifications, when I use the tool to run the Makefile from the submit directory, I receive the following output:

    Code:
    make: *** No rule to make target `CPUTimer.h', needed by `Proj4.o'.  Stop.
    This is after I followed your suggest and removed CPUTimer.cpp.

    Thanks for the help as well.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I would have guessed
    Code:
    $(PROJ): $(FREYS_DIR)/$(PROJ) $(OBJECTS)
    $(CC) $(CCFLAGS) -o $(PROJ)  $(OBJECTS)
    
    Proj4.o: $(FREYS_DIR)/Proj4.cpp kary_heap.h CPUTimer.h
    $(CC) $(CCFLAGS) -c $(FREYS_DIR)/Proj4.cpp
    
    CPUTimer.o: CPUTimer.cpp CPUTimer.h
    $(CC) $(CCFLAGS) -c CPUTimer.cpp
    But why you seem to be trying to make a .h file as a target is beyond me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    I'm not entirely sure, either. As I said, I'm not too experienced with Makefiles and I'm just using a provided one as a reference.

    I seem to have the Makefile working the way it should, however, it will not use the CPUTimer object (in the code) unless I include the CPUTimer header in my Proj4 source file. This seems to imply that it's not linking the CPUTimer.cpp object properly. This is the Makefile as it currently stands:

    Code:
    $(PROJ): $(FREYS_DIR)/$(PROJ) $(OBJECTS)
    $(CC) $(CCFLAGS) -o $(PROJ)  $(OBJECTS)
    
    Proj4.o: Proj4.cpp kary_heap.h CPUTimer.o
    $(CC) $(CCFLAGS) -c Proj4.cpp
    
    CPUTimer.o: $(FREYS_DIR)/CPUTimer.cpp $(FREYS_DIR)/CPUTimer.h
    $(CC) $(CCFLAGS) -c $(FREYS_DIR)/CPUTimer.cpp
    The problem it's giving me now is:

    Code:
    Proj4.cpp: In function 'int main(int, char**)':
    Proj4.cpp:18: error: 'CPUTimer' was not declared in this scope
    As I said, this implies it's not including the CPUTimer.cpp or something.. I can't include the Header file in my Proj4.cpp, because this seems to keep me from actually using the functions within CPUTimer.cpp..

    Any additional help? Much appreciated, everyone.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    But why you seem to be trying to make a .h file as a target is beyond me.
    He's NOT, the message just means that the file is missing. So make tries to create it, and sees that there is no rule for it.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The problem is probably all those $(FREYS_DIR) references. That isn't pertinent to what you're doing. Remove them everywhere they occur.

    In fact, just try using this:

    Code:
    CXX           = g++
    WFLAGS        = -W -Wall
    DFLAGS        = -g
    OFLAGS        =
    PRFLAGS       =
    INCLUDE       =
    CXXFLAGS      = $(WFLAGS) $(DFLAGS) $(OFLAGS) $(PRFLAGS) $(INCLUDE)
    
    LIBS         =
    LDFLAGS       = $(LIBS) $(PRFLAGS) # Whoops, this was PROFFLAGS before, which was wrong
    
    SRCS            = # List your source files here
    INST_SRCS  = # List your instructor's files here, with full path to each file
    HDRS            = # List your header files here
    
    OBJS            = $(SRCS:.cpp=.o)
    
    TARGET         = # Name of your program here
    
    .PHONY: clean
    
    $(TARGET): $(OBJS) $(INST_SRCS)
        $(CXX) -o $@ $^ $(LDFLAGS)
    
    clean:
        rm -f $(OBJS) $(TARGET)
    
    .depend: $(SRCS) $(INST_SRCS) $(HDRS)
        $(CXX) -M $(SRCS) > .depend # Note the change here as well
    
    -include .depend
    I just whipped this up now, so there may be problems in it, but I think it's correct. Just drop your source and header files in the appropriate places, and put the name of your program in the TARGET variable.
    Last edited by brewbuck; 04-30-2007 at 06:21 PM.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    Thanks for all of the help, brewbuck. I'll try that in a moment and let you know.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by OWD2k7 View Post
    Thanks for all of the help, brewbuck. I'll try that in a moment and let you know.
    By the way, if this project includes some .cpp files that your instructor provides for you, DON'T just reference them by their full path. This will cause the "clean" function to blow away your instructor's files. Instead, COPY the files into your project directory.

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    Quote Originally Posted by brewbuck View Post
    By the way, if this project includes some .cpp files that your instructor provides for you, DON'T just reference them by their full path. This will cause the "clean" function to blow away your instructor's files. Instead, COPY the files into your project directory.
    I tried your Makefile, and it works the same way mine did; but it still gives me the same errors:

    Code:
    g++ -M Proj4.cpp /afs/umbc.edu/users/f/r/frey/pub/CMSC341/Proj4/CPUTimer.cpp
     kary_heap.h /afs/umbc.edu/users/f/r/frey/pub/CMSC341/Proj4/CPUTimer.h > .depend
    g++ -W -Wall -g      -c -o Proj4.o Proj4.cpp
    Proj4.cpp: In function 'int main(int, char**)':
    Proj4.cpp:23: error: 'CPUTimer' was not declared in this scope
    Proj4.cpp:23: error: expected `;' before 'timer'
    Proj4.cpp:76: error: 'timer' was not declared in this scope
    Proj4.cpp:83: error: 'timer' was not declared in this scope
    Proj4.cpp:91: error: 'timer' was not declared in this scope
    Proj4.cpp:101: error: 'timer' was not declared in this scope
    Proj4.cpp:108: error: 'timer' was not declared in this scope
    Proj4.cpp:116: error: 'timer' was not declared in this scope
    Proj4.cpp:121: error: 'timer' was not declared in this scope
    make: *** [Proj4.o] Error 1
    The problem is, I'm not allowed to copy the instructor's files into my directory. It's one of the stipulations of the project, and the biggest source of my headache.. The grading script deletes these files from my folder if it finds them.

    The odd thing is, even when I do copy the files to my directory, it still gives me the above error..

    EDIT: I should also add that putting the following header into my Proj4.cpp file:

    Code:
    #include "/afs/umbc.edu/users/f/r/frey/pub/CMSC341/Proj4/CPUTimer.h"
    Will remove those errors, but then when I run the program, the output for the CPUTimer is always 0.
    Last edited by OWD2k7; 04-30-2007 at 06:15 PM. Reason: Adding on additional information.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by OWD2k7 View Post
    I tried your Makefile, and it works the same way mine did; but it still gives me the same errors:
    These are just programming errors, not related to the makefile. You seem to not be including a proper .h somewhere.

    The problem is, I'm not allowed to copy the instructor's files into my directory. It's one of the stipulations of the project, and the biggest source of my headache.. The grading script deletes these files from my folder if it finds them.
    Is the purpose of this assignment to learn how to write makefiles? If not, I think this requirement is stupid. But I guess it is what it is. In that case, have a look at my original post -- I'll modify the makefile to try to allow you to build from your instructor's files directly.

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, 09:45 AM
  2. Need help with makefile
    By New_Programmer in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2009, 04:55 PM
  3. Building a project using a Makefile
    By starcatcher in forum Windows Programming
    Replies: 2
    Last Post: 11-23-2008, 11:50 PM
  4. C Makefile Help needed
    By MasteAceVentura in forum C Programming
    Replies: 3
    Last Post: 03-12-2008, 08:54 PM
  5. Need help with Makefile
    By xshapirox in forum C++ Programming
    Replies: 14
    Last Post: 09-28-2004, 03:32 PM