I have been trying to clean up my odometry code and decided to move it to a separate file. I gave it a header file in the include folder where I define a namespace and an odometry class and declares all the public functions and variables. I then have the odometry.cpp file in src which just initializes the functions and performance the calculations. When I try to call them from main, I get the error from odometry.o ‘undefined reference to’ and then a list of every variable and function I declared it the header file.
#include <iostream>
using namespace std;
#pragma once
namespace MyRobot
{
extern semaphore GlobalPosSemaphore;
struct OdometryConfig
{
OdometryConfig(double wheel_travel, double tr, double tb);
double Wheel_travel;
double Tr;
double Tb;
};
class RotationSensors
{
public:
static void UpdateSensorValues(double wheelTravel);
static double TotalY;
static double TotalX;
static double DeltaY;
static double DeltaX;
};
class Odometry
{
public:
Odometry(OdometryConfig config);
void StartTrackingTask();
double globalHeading;
Point GlobalCoords;
private:
static double _wheelTravel;
static double _prevHeadingRads;
static double _tr;
static double _tb;
static int StartTracking();
};
} ```
Make sure you’re including the .h file, instead of the .cpp file.
Try to put the #pragma once
at the very top, and having a using namespace
in the global scope of a header file is bad practice. Also try to have the .h and .cpp in the same dir because of the way translation units are linked.
1 Like
Ok I thought to make file required the header files to be in include and the .cpp files in src? I am using the vscode extension in case that makes a difference.
Here is the odometer.cpp code:
#include "vex.h"
using namespace MyRobot;
OdometryConfig::OdometryConfig(double wheel_travel, double tr, double tb)
{
this->Wheel_travel = wheel_travel;
this->Tr = tr;
this->Tb = tb;
}
void RotationSensors::UpdateSensorValues(double wheelTravel)
{
double PrevY = 0.0;
double PrevX = 0.0;
double totalDegY = RotationY.position(deg);
double totalDegX = RotationX.position(deg);
TotalY = (totalDegY/360) * wheelTravel;
TotalX = (totalDegX/360) * wheelTravel;
DeltaY = TotalY - PrevY;
DeltaX = TotalX - PrevX;
}
Odometry::Odometry(OdometryConfig config)
{
_wheelTravel = config.Wheel_travel;
_prevHeadingRads = 0.0;
_tr = config.Tr;
_tb = config.Tb;
}
int Odometry::StartTracking()
{
double globalHeading;
double globalHeadingRads;
Point GlobalCoords;
while(true)
{
RotationSensors::UpdateSensorValues(_wheelTravel);
globalHeadingRads = Functions::toRad(Inert.heading(deg));
double deltaHeadingRads = globalHeadingRads - _prevHeadingRads;
GlobalPosSemaphore.lock(3000);
globalHeading = Inert.heading(deg);
GlobalPosSemaphore.unlock();
double localY = (2 * sin(deltaHeadingRads/2)) * ((RotationSensors::DeltaY/deltaHeadingRads) + _tr);
double localX = (2 * sin(deltaHeadingRads/2)) * ((RotationSensors::DeltaX/deltaHeadingRads) + _tb);
Point localCoords = {localX, localY};
PolarCoord polarCoord = Functions::toPolar(localCoords);
polarCoord.angle = polarCoord.angle - globalHeadingRads;
Point changeInPos = Functions::toCartesian(polarCoord);
GlobalCoords.x += changeInPos.x;
GlobalCoords.y += changeInPos.y;
wait(12, msec);
}
return 0;
}
void Odometry::StartTrackingTask()
{
task odometryTask = task(StartTracking);
}
Here are the errors:
"CXX src/odometry.cpp"
"LINK build/HighStakes.elf"
build/src/main.o: In function `autonomous()':
main.cpp:(.text._Z10autonomousv+0x4c): undefined reference to `MyRobot::GlobalPosSemaphore'
main.cpp:(.text._Z10autonomousv+0x5c): undefined reference to `MyRobot::GlobalPosSemaphore'
build/src/odometry.o: In function `MyRobot::OdometryConfig::OdometryConfig(double, double, double)':
odometry.cpp:(.text._ZN7MyRobot14OdometryConfigC2Eddd+0x4): undefined reference to `MyRobot::OdometryConfig::Wheel_travel'
odometry.cpp:(.text._ZN7MyRobot14OdometryConfigC2Eddd+0x8): undefined reference to `MyRobot::OdometryConfig::Wheel_travel'
odometry.cpp:(.text._ZN7MyRobot14OdometryConfigC2Eddd+0x18): undefined reference to `MyRobot::OdometryConfig::Tr'
odometry.cpp:(.text._ZN7MyRobot14OdometryConfigC2Eddd+0x1c): undefined reference to `MyRobot::OdometryConfig::Tr'
odometry.cpp:(.text._ZN7MyRobot14OdometryConfigC2Eddd+0x28): undefined reference to `MyRobot::OdometryConfig::Tb'
odometry.cpp:(.text._ZN7MyRobot14OdometryConfigC2Eddd+0x2c): undefined reference to `MyRobot::OdometryConfig::Tb'
build/src/odometry.o: In function `MyRobot::RotationSensors::UpdateSensorValues(double)':
odometry.cpp:(.text._ZN7MyRobot15RotationSensors18UpdateSensorValuesEd+0x50): undefined reference to `MyRobot::RotationSensors::TotalY'
odometry.cpp:(.text._ZN7MyRobot15RotationSensors18UpdateSensorValuesEd+0x58): undefined reference to `MyRobot::RotationSensors::TotalY'
odometry.cpp:(.text._ZN7MyRobot15RotationSensors18UpdateSensorValuesEd+0x6c): undefined reference to `MyRobot::RotationSensors::DeltaY'
odometry.cpp:(.text._ZN7MyRobot15RotationSensors18UpdateSensorValuesEd+0x70): undefined reference to `MyRobot::RotationSensors::DeltaY'
odometry.cpp:(.text._ZN7MyRobot15RotationSensors18UpdateSensorValuesEd+0x78): undefined reference to `MyRobot::RotationSensors::TotalX'
odometry.cpp:(.text._ZN7MyRobot15RotationSensors18UpdateSensorValuesEd+0x7c): undefined reference to `MyRobot::RotationSensors::TotalX'
odometry.cpp:(.text._ZN7MyRobot15RotationSensors18UpdateSensorValuesEd+0x84): undefined reference to `MyRobot::RotationSensors::DeltaX'
odometry.cpp:(.text._ZN7MyRobot15RotationSensors18UpdateSensorValuesEd+0x88): undefined reference to `MyRobot::RotationSensors::DeltaX'
build/src/odometry.o: In function `MyRobot::Odometry::Odometry(MyRobot::OdometryConfig)':
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x0): undefined reference to `MyRobot::Odometry::_prevHeadingRads'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x8): undefined reference to `MyRobot::Odometry::_prevHeadingRads'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0xc): undefined reference to `MyRobot::OdometryConfig::Tb'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x14): undefined reference to `MyRobot::OdometryConfig::Tb'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x1c): undefined reference to `MyRobot::OdometryConfig::Wheel_travel'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x20): undefined reference to `MyRobot::OdometryConfig::Wheel_travel'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x24): undefined reference to `MyRobot::Odometry::_wheelTravel'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x2c): undefined reference to `MyRobot::OdometryConfig::Tr'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x30): undefined reference to `MyRobot::Odometry::_wheelTravel'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x34): undefined reference to `MyRobot::OdometryConfig::Tr'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x3c): undefined reference to `MyRobot::Odometry::_tr'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x44): undefined reference to `MyRobot::Odometry::_tr'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x50): undefined reference to `MyRobot::Odometry::_tb'
odometry.cpp:(.text._ZN7MyRobot8OdometryC2ENS_14OdometryConfigE+0x54): undefined reference to `MyRobot::Odometry::_tb'
build/src/odometry.o: In function `MyRobot::Odometry::StartTracking()':
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0x3c): undefined reference to `MyRobot::Odometry::_wheelTravel'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0x40): undefined reference to `MyRobot::Odometry::_wheelTravel'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0x68): undefined reference to `MyRobot::GlobalPosSemaphore'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0x70): undefined reference to `MyRobot::Odometry::_prevHeadingRads'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0x74): undefined reference to `MyRobot::GlobalPosSemaphore'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0x78): undefined reference to `MyRobot::Odometry::_prevHeadingRads'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0xc4): undefined reference to `MyRobot::Odometry::_tr'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0xc8): undefined reference to `MyRobot::Odometry::_tr'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0xd4): undefined reference to `MyRobot::RotationSensors::DeltaY'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0xd8): undefined reference to `MyRobot::RotationSensors::DeltaY'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0xec): undefined reference to `MyRobot::RotationSensors::DeltaX'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0xf4): undefined reference to `MyRobot::RotationSensors::DeltaX'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0x100): undefined reference to `MyRobot::Odometry::_tb'
odometry.cpp:(.text._ZN7MyRobot8Odometry13StartTrackingEv+0x10c): undefined reference to `MyRobot::Odometry::_tb'
make: *** [vex/mkrules.mk:18: build/HighStakes.elf] Error 1
Make process closed with exit code: 2
it doesn’t look like you include the header file in your odometry cpp file, you need to include it so the cpp file knows what the class looks like. Also, like Joeger_Bahar said put the #pragma once
as the very first line in the header file. Having the header files in include and cpp files in src is fine.
You also never define GlobalPosSemaphore
. You declare it in the header file, but the extern
keyword tells the program to not actually define it yet, then you never end up defining it later. Either get rid of the extern keyword or define it in your cpp file.
4 Likes