So I’ve been working with this problem for while now and what I think is happening is that my three structs are being defined multiple times because the definitions are in a header file that is included in every other file. I have tried using #ifndef to no avail. Here is custom.h which is included in main.h, which is in turn included in most other files. BL is abbreviated for BottomLeft, BR for BottomRight, T for Top.
//Motors
#define liftLeft 2
#define liftRight 9
#define chainLeft 3
#define chainRight 8
//sensors
#define BLPot 1
#define BRPot 2
#define QUAD1 1
#define QUAD2 2
//PID structs
#ifndef TEST0
#define TEST0 1
typedef struct NAME{
double Kp;
double Ki;
double Kd;
double error;
double previous_error;
double integral;
double derivative;
double target;
double sensor;
}PID;
#endif
#ifndef TEST1
#define TEST1 1
PID BL = {
.Kp = 0, .Ki = 0, .Kd = 0, .error = 0, .previous_error = 0, .integral = 0, .derivative = 0, .target = 0, .sensor = 0
};
#endif
#ifndef TEST2
#define TEST2 1
PID BR = {
.Kp = 0, .Ki = 0, .Kd = 0, .error = 0, .previous_error = 0, .integral = 0, .derivative = 0, .target = 0, .sensor = 0
};
#endif
#ifndef TEST3
#define TEST3 1
PID T = {
.Kp = 0, .Ki = 0, .Kd = 0, .error = 0, .previous_error = 0, .integral = 0, .derivative = 0, .target = 0, .sensor = 0
};
#endif
//thread prototypes
void mainLoop();
void debug();
And here is debug.c, which is one of the place where the compiler complains about multiple definitions:
#include "main.h"
void debug(){
printf("\n\n\n\n\n");
printf("Sensors: LPot:%d RPot:%d mainQuad:%d\n", analogRead(BLPot), analogRead(BRPot), encoderGet(mainQuad));
printf("BL: Error:%f Deriv:%f Integral:%f\n", BL.error, BL.derivative, BL.integral);
printf("BR: Error:%f Deriv:%f Integral:%f\n", BR.error, BR.derivative, BR.integral);
printf("T: Error:%f Deriv:%f Integral:%f\n", T.error, T.derivative, T.integral);
}
And here’s the erros that get spit out when I run make:
CC -I../include -I../src PID.c
CC -I../include -I../src auto.c
CC -I../include -I../src debug.c
CC -I../include -I../src functions.c
CC -I../include -I../src init.c
init.c: In function 'initialize':
init.c:53:14: warning: unused variable 'secondTH' -Wunused-variable]
init.c:47:7: warning: variable 'T' set but not used -Wunused-but-set-variable]
init.c:44:7: warning: variable 'BR' set but not used -Wunused-but-set-variable]
init.c:41:7: warning: variable 'BL' set but not used -Wunused-but-set-variable]
CC -I../include -I../src opcontrol.c
LN ./bin/PID.o ./bin/auto.o ./bin/debug.o ./bin/functions.o ./bin/init.o ./bin/opcontrol.o ./firmware
/libpros.a -lgcc -lm to bin/output.elf
./bin/debug.o:(.bss+0x90): multiple definition of `T'
./bin/PID.o:(.bss+0x90): first defined here
./bin/debug.o:(.bss+0x48): multiple definition of `BR'
./bin/PID.o:(.bss+0x48): first defined here
./bin/debug.o:(.bss+0x0): multiple definition of `BL'
./bin/PID.o:(.bss+0x0): first defined here
./bin/functions.o:(.bss+0x0): multiple definition of `T'
./bin/PID.o:(.bss+0x90): first defined here
./bin/functions.o:(.bss+0x48): multiple definition of `BR'
./bin/PID.o:(.bss+0x48): first defined here
./bin/functions.o:(.bss+0x90): multiple definition of `BL'
./bin/PID.o:(.bss+0x0): first defined here
./bin/init.o:(.bss+0x0): multiple definition of `T'
./bin/PID.o:(.bss+0x90): first defined here
./bin/init.o:(.bss+0x48): multiple definition of `BR'
./bin/PID.o:(.bss+0x48): first defined here
./bin/init.o:(.bss+0x90): multiple definition of `BL'
./bin/PID.o:(.bss+0x0): first defined here
./bin/opcontrol.o:(.bss+0x90): multiple definition of `T'
./bin/PID.o:(.bss+0x90): first defined here
./bin/opcontrol.o:(.bss+0x0): multiple definition of `BR'
./bin/PID.o:(.bss+0x48): first defined here
./bin/opcontrol.o:(.bss+0x48): multiple definition of `BL'
./bin/PID.o:(.bss+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
make.exe": *** [bin/output.elf] Error 1