Multiple definitiions VEXCode

Hello - I’m getting the ‘multiple definition’ error when compiling. I have searched every file in the project folder and am not having any luck - was hoping someone could point me in a new direction…

build/src/robot-config.o:(.bss.Brain+0x0): multiple definition of `Brain'
build/src/main.o:(.bss.Brain+0x0): first defined here
build/src/robot-config.o:(.bss.Controller1+0x0): multiple definition of `Controller1'
build/src/main.o:(.bss.Controller1+0x0): first defined here

…a BUNCH of these…

ie: Brain declaration:
robot-config.h extern brain Brain;
robot-config…cpp brain Brain;
src/main NO mention of Brain

This might be helpful:

Basically, if you #include a file with a definition of something, the compiler with see that as being defined multiple times. Instead of defining things in files that will be included, they should be declared.

Using extern should work, so if it doesn’t, you might be having another problem. I’m not familiar with VEXCode, but I would guess it should be extern Brain brain; and Brain brain; instead of, extern brain Brain and brain Brain (notice capitalization), because classes are typically capitalized.

If either of those don’t fix the problem, I think we will need more of you code to help.

Thanks Carlos - got it figured out but appreciate you thoughts…

I’m trying to move the user-defined variables / functions() in a separate file
…in order for main.cpp to ‘see’ it I put an include in there:
#include "user-prog.cpp"
…causing all of the errors I mentioned…

So created
…and moved the code to it (changing the #include in main) and it works!

Q is src/ seems a better place for this - how would I make main.cpp aware of a file in the src folder?

Thanks a million!!

Usually you shouldn’t #include .cpp files. When using multiple files, it is pretty standard to have the declarations in the header (.h) file and the definitions in the source file (.cpp).

I don’t really know what code you are trying to make, but here is an example of a function that spins a motor at a speed specified as a parameter:

In user-prog.h:

void spinMotor(double motorValue); // declaration

In user-prog.cpp:

#include "include/user-prog.h"

void spinMotor(double motorValue) { // definition
    motor.spin(motorValue); // I'm not sure this is exactly how you move a motor in VEXCode, but you get the picture

In main.cpp:

#include "include/user-prog.h"

spinMotor (75); 

When the compiler builds all of the source files separately, it includes user-prog.h to main.h, so it knows main.h has access to spinMotor, even though it doesn’t yet know exactly what spinMotor does. When it builds user-prog.h, it sees the definition of what spinMotor does, so when the code is run, it knows what to do. Here is some more information about this:

I hope that answers your question.


THANK YOU! I can’t tell you how much I appreciate this nor how long I’ve struggled!!

Now I know!!