C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-05-2009, 07:45 AM   #1
NOX
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
NOX is offline   Reply With Quote
Old 10-05-2009, 09:26 AM   #2
a_capitalist_story
 
Join Date: Dec 2007
Posts: 993
So...how does the Makefile know how to make Road.o? Or any of the other .o files?
rags_to_riches is offline   Reply With Quote
Old 10-05-2009, 09:34 AM   #3
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,029
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.
bithub is offline   Reply With Quote
Old 10-05-2009, 10:57 AM   #4
Registered User
 
Join Date: Apr 2006
Posts: 1,337
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.
King Mir is offline   Reply With Quote
Old 10-05-2009, 11:06 AM   #5
NOX
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?
NOX is offline   Reply With Quote
Old 10-05-2009, 11:13 AM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 10,163
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.
tabstop is offline   Reply With Quote
Old 10-05-2009, 11:24 AM   #7
Registered User
 
Join Date: Apr 2006
Posts: 1,337
Quote:
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.
King Mir is offline   Reply With Quote
Old 10-05-2009, 11:34 AM   #8
NOX
Registered User
 
Join Date: Oct 2009
Posts: 3
Ok, I understand now, thank you very much ^_^

-NOX
NOX is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dabbling with Linux. Hunter2 Tech Board 21 04-21-2005 04:17 PM
Binary Search Trees Part III Prelude A Brief History of Cprogramming.com 16 10-02-2004 03:00 PM
Compile my code for Linux? CompiledMonkey Linux Programming 5 11-17-2002 02:02 AM
<< !! Posting Code? Read this First !! >> kermi3 Linux Programming 0 10-14-2002 01:30 PM
Can you have nested code block? What does the compiler do? For example ... albertr C Programming 4 01-16-2002 12:04 AM


All times are GMT -6. The time now is 12:20 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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