Problems linking C++ files???

Hello to all that read this…

I’m having some problems linking my files together. I have written a couple of classes and have written the following makefile for this set of files:

OBJECTS = Timers.o baseThread.o controlThread.o displayThread.o ThreadProj.o
CFLAGS = -o proj

proj: $(OBJECTS)

Timers.o: Timers.h
QCC -o Timers.o

baseThread.o: baseThread.h

QCC -o baseThread.o

controlThread.o: controlThread.h baseThread.h Timers.h
QCC -o controlThread.o

displayThread.o: displayThread.h baseThread.h Timers.h
QCC -o displayThread.o

ThreadProj.o: displayThread.h controlThread.h
QCC -o ThreadProj.o

Timers.o baseThread.o controlThread.o displayThread.o: Timers.h

baseThread.o controlThread.o displayThread.o: baseThread.h

ThreadProj.o controlThread.o: controlThread.h

ThreadProj.o displayThread.o: displayThread.h

When the makefile is launched the compilation terminates with the following errors:

make: Entering directory /home/COEN320/QNX_Project/Proj' QCC -o controlThread.o /tmp/AAA149288_cc.o: In function controlThread::controlThread(int)’:
/tmp/AAA149288_cc.o(.text+0xf): undefined reference to baseThread::baseThread(void)' /tmp/AAA149288_cc.o(.text+0x2b): undefined reference to Timers::Timers(void)’
/tmp/AAA149288_cc.o(.text+0x6d): undefined reference to Timers::~Timers(void)' /tmp/AAA149288_cc.o(.text+0x7f): undefined reference to baseThread::~baseThread(void)’
/tmp/AAA149288_cc.o: In function controlThread::~controlThread(void)': /tmp/AAA149288_cc.o(.text+0xd2): undefined reference to Timers::~Timers(void)’
/tmp/AAA149288_cc.o(.text+0xe2): undefined reference to baseThread::~baseThread(void)' /tmp/AAA149288_cc.o: In function controlThread::run1(void)’:
/tmp/AAA149288_cc.o(.text+0x31f): undefined reference to Timers::showTimeStats(void)' /tmp/AAA149288_cc.o(.text+0x331): undefined reference to Timers::setStartCycle(void)’
/tmp/AAA149288_cc.o(.text+0x343): undefined reference to Timers::setStart(void)' /tmp/AAA149288_cc.o(.text+0x42b): undefined reference to Timers::getStart(void)’
/tmp/AAA149288_cc.o(.text+0x445): undefined reference to Timers::showTimeSpan(timespec)' /tmp/AAA149288_cc.o(.text+0x45a): undefined reference to Timers::getStartCycle(void)’
/tmp/AAA149288_cc.o(.text+0x46c): undefined reference to Timers::showElapsedCycles(int)' /tmp/AAA149288_cc.o: In function controlThread type_info function’:
/tmp/AAA149288_cc.o(.gnu.linkonce.t.__tf13controlThread+0x10): undefined reference to baseThread type_info function' /tmp/AAA149288_cc.o(.gnu.linkonce.t.__tf13controlThread+0x1a): undefined reference to baseThread type_info node’
cc: /usr/bin/ntox86-ld error 1
make: *** [controlThread.o] Error 1
make: Leaving directory `/home/COEN320/QNX_Project/Proj’

All the header files are properly referenced in their respective source files. I’ve tried many changes (g++ gives same results) but no luck. Although it doesn’t appear in the error dump the first two files (i.e. and compile ok, and then all hell breaks loose. Anyone with any ideas of what’s wrong? Any feedback would be greatly appreciated.


Sure, you want to have your rule say:

controlThread.o: controlThread.h baseThread.h Timers.h
QCC -c

The important point here is that you use -c to tell the compiler front end to compile to an object, and you use -o to tell the front end to link to an executable. The error was because you were telling it to link and there are a bunch of unresolved references.

There are other problems in this makefile (assuming it is copied correctly). In general, you could simplify it something like this:

OBJECTS = Timers.o baseThread.o controlThread.o displayThread.o ThreadProj.o

proj: $(OBJECTS)
QCC -o proj $(OBJECTS)

Timers.o: Timers.h

baseThread.o: baseThread.h

controlThread.o: controlThread.h baseThread.h Timers.h

displayThread.o: displayThread.h baseThread.h Timers.h

ThreadProj.o: displayThread.h controlThread.h

I think that is all you need. The default rules should take care of the rest. Caveat is that this is off the top of my head and may not work exactly as written. :slight_smile: