I’m new to C++, switching over from Java for a class I’m taking in college. In our first assignment we have to write several makefiles for our code. My first makefile isn’t working – it always complains about an error in “TempControlTest: undefined reference to ‘BangBangControl::XXXX’, where XXXX a function in BangBangControl (I get errors for every function in BangBangControl). I’ve been looking around online and all the makefile examples look slightly different so I can’t tell what I’m doing wrong. I’ll post all my code below, if anyone could just give me a little help, that would be AWESOME. Also, this is due tonight, so any quick help is appreciated so much. If you’re still here, thanks very much!
HeatingUnit.h:
#ifndef HEATINGUNIT_H
#define HEATINGUNIT_H
class HeatingUnit {
private:
bool on;
int temp;
public:
HeatingUnit(bool o, int t);
void turnOn();
void turnOff();
int tick();
};
#endif
HeatingUnit.cpp:
#include "HeatingUnit.h"
HeatingUnit::HeatingUnit(bool o, int t) {
on = o;
temp = t;
}
void HeatingUnit::turnOn() {
on = true;
}
void HeatingUnit::turnOff() {
on = false;
}
int HeatingUnit::tick() {
if(on) return ++temp + 1;
else return --temp;
}
BangBangControl.h:
#ifndef BANGBANGCONTROL_H
#define BANGBANGCONTROL_H
#include "HeatingUnit.h"
class BangBangControl {
private:
int tempToKeep;
HeatingUnit unit;
public:
BangBangControl(int ttk, bool on, int initTemp);
void setTemp(int t);
int getTemp();
void update();
};
#endif
BangBangControl.cpp:
#include "HeatingUnit.h"
#include "BangBangControl.h"
BangBangControl::BangBangControl(int ttk, bool on, int initTemp)
: unit(on, initTemp) {
tempToKeep = ttk;
}
void BangBangControl::setTemp(int t) {
tempToKeep = t;
}
int BangBangControl::getTemp() {
return tempToKeep;
}
void BangBangControl::update() {
int currentTemp = unit.tick();
if(currentTemp > tempToKeep + 2) unit.turnOff();
else if(currentTemp < tempToKeep - 2) unit.turnOn();
}
TempControlTest.cpp:
include <iostream>
#include "BangBangControl.h"
using namespace std;
int main() {
BangBangControl bang(100, false, 60);
for(int i = 0; i < 50; i++) {
bang.update();
cout << bang.getTemp() << " ";
if(i % 10 == 9) cout << endl;
}
}
And finally, Makefile_Executable:
all: TempControlTest
TempControlTest: TempControlTest.cpp BangBangControl.o
g++ -o TempControlTest TempControlTest.cpp
BangBangControl.o: BangBangControl.cpp HeatingUnit.o
g++ -c BangBangControl.cpp
HeatingUnit.o: HeatingUnit.cpp
g++ -c HeatingUnit.cpp
clean:
rm -rf *.o TempControlTest
Again, thank you very much!
You aren’t linking your objects together.
You’ve got two lines compiling
BangBangControl.cppandHeatingUnit.cppto two.ofiles (although I doubt you need to haveBangBangControl.odepend onHeatingUnit.o). But you need to put them together into the final executable (this is called “linking”).In the line above,
TempControlTest.cppwill be itself compiled, and then linked together with the two already-compiled objects on which it seems to depend into one executable.NB: when you get an “Undefined reference” error, it generally means your code compiles fine but was missing a symbol it needed to be linked against.
NB2: your makefile rules don’t cause the cpp files to depend on the headers they use. That means when you change a header and run “make”, make will tell you everything’s already built. That’s bad.