Thread: Linux G++ Makefile (Or C++ Code)

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Linux G++ Makefile (Or C++ Code)

    On my Linux VM I have everything installed correctly and updated properly. I really am new to Makefiles, so before you point me to a tutorial I just need to understand why my code is not compiling.

    Actually, I can make it compile, but what I have to do does not seem right at all. What I do to "fix" this error is make "Queue.o" a dependency in main, but it shouldn't depend on the Queue class...For now it should just depend on the road class...

    Error:
    Code:
    Road.o: In function 'Road::Road(int, int)':
    Road.C: (.text+0x56): undefined reference to 'Queue::Queue(char)'
    Road.o: In function 'Road::Road(int, int)':
    Road.C: (.text+0x86): undefined reference to 'Queue::Queue(char)'
    collect2: ld returned 1 exit status
    make *** [main] Error 1
    Makefile:
    Code:
    CXXFLAGS=-O3
    
    all: main
    
    main: Road.o
    
    Queue.o: Queue.h Vehicle.o
    
    Road.o: Road.h
    
    Vehicle.o: Vehicle.h
    
    clean:
        rm *.o main
    Queue Class Header:
    Code:
    #ifndef _QUEUE_H_
    #define _QUEUE_H_
    
    #include "Vehicle.h"
    #include <cstdlib>
    
    struct QNode;
    
    class Queue{
    public:
        /* CONSTRUCTORS */
        Queue();
    
        /* OBSERVERS */
        bool IsEmpty() const{return pHead==NULL;};
        int  Size() const{return size;};
        void Print() const;
    
        /* TRANSFORMERS */
        void Dequeue();
        void Enqueue(Vehicle * pV);
    
    private:
        QNode * pHead;
        QNode * pTail;
        int     size;
    };
    
    #endif
    Road Class Header:
    Code:
    #ifndef _ROAD_H_
    #define _ROAD_H_
    
    #include "Queue.h"
    
    class Road{
    public:
        /* CONSTRUCTORS */
        Road(int id, int travelTime = (rand() % 20) + 1);
    
        /* OBSERVERS */
        int  GetID() const{return _id;};
        int  GetTravelTime() const{return _travelTime;};
        void Print() const;
    
        /* TRANSFORMERS */
        void AddVehicle();
        void RemoveVehicle();
        void SetID(int newID);
        void SetTravelTime(int newTime);
    
    private:
        Queue _road;
        int   _id;
        int   _travelTime;
    };
    
    #endif

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So...how does the Makefile know how to make Road.o? Or any of the other .o files?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your makefile should probably look more like this:
    Code:
    CXXFLAGS=-O3
    
    all: main
    
    main: Road.o
    
    Queue.o: Queue.cpp Vehicle.o
    
    Road.o: Road.cpp
    
    Vehicle.o: Vehicle.cpp
    
    clean:
        rm *.o main
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I disagree with bithub.

    You want the object files to depend on both their associated source files, and their included non library headers.

    You don't want the object files to depend on each other.

    Main should depend on all of the object files.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Quote Originally Posted by rags_to_riches View Post
    So...how does the Makefile know how to make Road.o? Or any of the other .o files?
    Quote Originally Posted by bithub View Post
    Your makefile should probably look more like this:
    Code:
    CXXFLAGS=-O3
    
    all: main
    
    main: Road.o
    
    Queue.o: Queue.cpp Vehicle.o
    
    Road.o: Road.cpp
    
    Vehicle.o: Vehicle.cpp
    
    clean:
        rm *.o main
    I was always taught to use the header file (or specification file), because make is "smart" enough to understand that it is in fact just a header file.

    Quote Originally Posted by King Mir View Post
    I disagree with bithub.

    You want the object files to depend on both their associated source files, and their included non library headers.

    You don't want the object files to depend on each other.

    Main should depend on all of the object files.
    Ok, so your talking about modular code then (a.k.a. Code that only depends on itself). But my question would have to be if that Road depends on the Queue (because that all a road is, is a Queue), then why wouldn't I put that as a dependency?

    So pretty much what I am asking is, if Road depends on Queue, then why wouldn't I make it so in the Makefile?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It is highly unlikely that Road depends on Queue.o. (I'm assuming here that Road does not contain your main function.) It may well depend on Queue.h. It can call functions that live in Queue.o, which is not at all the same thing as depending on Queue.o.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Ok, so your talking about modular code then (a.k.a. Code that only depends on itself). But my question would have to be if that Road depends on the Queue (because that all a road is, is a Queue), then why wouldn't I put that as a dependency?

    So pretty much what I am asking is, if Road depends on Queue, then why wouldn't I make it so in the Makefile?
    Because if Road.cpp uses functions from Queue.cpp, then they are coded as references. The object file does not resolve those references; that's done at the linking stage, when main is built.

    Road.o does need to depend on Queue.h, because it includes that header.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Ok, I understand now, thank you very much ^_^

    -NOX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 04:17 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Compile my code for Linux?
    By CompiledMonkey in forum Linux Programming
    Replies: 5
    Last Post: 11-17-2002, 02:02 AM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM