Externing motors in PROS

I’m working on a project and I wanted to use multiple files, and I’m having trouble wrapping my head around this issue I’m running into. What I want to do is have my autonomous routines in a separate file, and I’m getting an error when trying to reference my motors.

In main.h I extern my motor:

extern pros::Motor LiftMotor;

In main.cpp I include my main.h header file and declare my motor (I’m not too familiar with the proper terminology so please correct me if I slip up)

#include main.h

pros::Motor LiftMotor (6, MOTOR_GEARSET_36, true, MOTOR_ENCODER_DEGREES);

Finally, in a separate file I’ve created for my autos, autos.cpp, I include main.h and use my motor:

#include main.h

void test_motor(){

The problem I’m having is that I get an error in autos.cpp on LiftMotor that says use of undeclared identifier 'LiftMotor'. Any help would be greatly appreciated!

What is happening is that first, the compiler would put all the contents of main.h at the top. Then when compiling, it works it’s way from top to bottom. When it reaches your extern statement, the compiler had not seen the declaration yet. Our colution to this is to make another hedder, and include your externs their. And in every cpp file EXCEPT the one the motors are declared in, include both, main.h and your new file

since PROS uses c++17 (with gnu extensions) by default, you can actually vastly simplify this:

can be replaced with

// main.h or whichever header file

inline pros::Motor LiftMotor(6, MOTOR_GEARSET_36, true, MOTOR_ENCODER_DEGREES);