Jason
Jason

Reputation: 123

How do I create a global variable among multiple files?

I am trying to add a simple boolean variable that I can change from multiple files. At least be able to change from marlin_main.cpp. I read I have to declare on a header file then define on one other file that includes the header and I should be able to use it.

I added

static bool endstopDisabled;

to the header file then declared it in its .cpp file

bool endstopDisabled;

It compiles fine but when I try to use it like this

endstopDisabled = false;

below in the same .cpp file it doesn't compile. It gives me the error "Error compiling for board Sanguino.". Anyone know what I am doing wrong? All I need is a boolean variable accessible to 2 files: Marlin_main.cpp and endstops.cpp. I am declaring variable in endstops.h.

Thank you in advance.

Upvotes: 0

Views: 81

Answers (1)

Jason
Jason

Reputation: 123

I fixed it. I had to declare the variable in endstops.cpp with bool Endstops::endstopDisabled; instead.

Upvotes: 1

Related Questions