Build errors

I’m trying to build my project, and Vexcode says there are no issues with my code and I am up to date on Vexcode version, but before it can build it always has this message.

build/src/Odometery.o:(.bss.global_theta+0x0): multiple definition of `global_theta’

build/src/main.o:(.bss.global_theta+0x0): first defined here

build/src/Odometery.o:(.bss.x_position+0x0): multiple definition of `x_position’

build/src/main.o:(.bss.x_position+0x0): first defined here

build/src/Odometery.o:(.bss.y_position+0x0): multiple definition of `y_position’

build/src/main.o:(.bss.y_position+0x0): first defined here

build/src/PID.o:(.bss.global_theta+0x0): multiple definition of `global_theta’

build/src/main.o:(.bss.global_theta+0x0): first defined here

build/src/PID.o:(.bss.x_position+0x0): multiple definition of `x_position’

build/src/main.o:(.bss.x_position+0x0): first defined here

build/src/PID.o:(.bss.y_position+0x0): multiple definition of `y_position’

build/src/main.o:(.bss.y_position+0x0): first defined here

build/src/Pure_Pursuit.o:(.bss.global_theta+0x0): multiple definition of `global_theta’

build/src/main.o:(.bss.global_theta+0x0): first defined here

build/src/Pure_Pursuit.o:(.bss.x_position+0x0): multiple definition of `x_position’

build/src/main.o:(.bss.x_position+0x0): first defined here

build/src/Pure_Pursuit.o:(.bss.y_position+0x0): multiple definition of `y_position’

build/src/main.o:(.bss.y_position+0x0): first defined here

build/src/Driver_Control.o:(.bss.global_theta+0x0): multiple definition of `global_theta’

build/src/main.o:(.bss.global_theta+0x0): first defined here

build/src/Driver_Control.o:(.bss.x_position+0x0): multiple definition of `x_position’

build/src/main.o:(.bss.x_position+0x0): first defined here

build/src/Driver_Control.o:(.bss.y_position+0x0): multiple definition of `y_position’

build/src/main.o:(.bss.y_position+0x0): first defined here

make: *** [vex/mkrules.mk:18: build/MecanumDrive.elf] Error 1

My code for the Heading files are as followed:

Odometery.h

#include "QOL_functions.h"

double x_position = 0;
double y_position = 0;
double global_theta;

extern int get_postion();```

DriverControl.h

```#include "vex.h"

double atan_unit_circle(double x, double y);
double find_relative(double angle_value);
void driver_control();```

PID.h

```#include "vex.h"
#include <vector>
#include "Odometery.h"
#include "QOL_functions.h"

using namespace vex;

int PID_drive(double end_x, double end_y, double kP, double kI, double kD );
void PID_strafe(double end_x, double end_y,double kP, double kI, double kD);
int PID_turn(double end_angle, double kP, double kI, double kD);
int PID_turn_index(double end_angle, double kP, double kI, double kD);

std::vector<double> pure_PID(double current_x, double current_y, double end_x, double end_y, double prev_error, double total_error, double kP, double kI, double kD);```

Pure_Pursuit.h

```#include "vex.h"
#include "PID.h"
#include "path_data.h"
#include <vector>

using namespace vex;

extern std::vector<std::vector<double>> circle_line_intersection(double start_x, double start_y, double end_x, double end_y, double center_x, double center_y,double lookahead);

extern std::vector<double> find_lookahead(std::vector<double> x,std::vector<double> y,double center_x,double center_y,double lookahead, int start_index, std::vector<double> previous_lookahead = {0,0,0} );

extern double pure_pursuit(std::vector<double> path_x, std::vector<double> path_y, double lookahead, double target_velocity, double track_width);```

main.cpp (top part)

```#include "vex.h"
#include "Driver_Control.h"
#include "Pure_pursuit.h"
#include "path_data.h"```



type or paste code here

This is caused by one h file getting imported from multiple places, so cpp is stupid and tries to redefine the stuff in the h file, then it gets mad at you for defining stuff twice. You should be able to fix that by putting #pragma once at the top of your h files, or by doing this in them:

#ifndef _MYHEADER_H_
#define _MYHEADER_H_

// Your header stuff here…

#endif

And replace _MYHEADER_H_ with something different for each header file

1 Like

In addition to what @iseau395 mentioned…
you have…

extern std::vector<std::vector<double>> circle_line_intersection(double start_x, double start_y, double end_x, double end_y, double center_x, double center_y,double lookahead);

extern std::vector<double> find_lookahead(std::vector<double> x,std::vector<double> y,double center_x,double center_y,double lookahead, int start_index, std::vector<double> previous_lookahead = {0,0,0} );

extern double pure_pursuit(std::vector<double> path_x, std::vector<double> path_y, double lookahead, double target_velocity, double track_width);

contained in Pure_Pursuit.h.

This tells the compiler/builder that somewhere in your c++ files there are 3 objects (find_lookahead, pure_pursuit, and circle_line_intersection)

However, you must still define those 3 objects in a .cpp file in addition to a .h file (which you already have).

Essentially, you must create another file, perhaps Pure_Pursuit.cpp with these lines in it:

// if you have all the appropriate includes in vex.h:
#include "vex.h" 

std::vector<std::vector<double>> circle_line_intersection(double start_x, double start_y, double end_x, double end_y, double center_x, double center_y,double lookahead);

std::vector<double> find_lookahead(std::vector<double> x,std::vector<double> y,double center_x,double center_y,double lookahead, int start_index, std::vector<double> previous_lookahead = {0,0,0} );

double pure_pursuit(std::vector<double> path_x, std::vector<double> path_y, double lookahead, double target_velocity, double track_width);

I am fairly certain this should do it, please let me know if this doesn’t work. I have had this issue many times. :+1:

2 Likes

Thanks for the help, but could you elaborate on what the MYHEADER_H example is, so would I put #ifndef _Odometery_H_

for Odometery or something else

Yup, that’s how you do it, but I’d recommend making it all caps because macros are generally in all caps (it’s not required to work though).