Multi File Sketch (.ino and .h) Global Variable in .ino is not in scope in .h

(deleted)

You have to declare the variable in the .h file even if it isn’t defined there.

extern int numb;

Google C++ extern for more info.

Or, define the variable BEFORE including the .h file.

int numb = 0;

#include "u.h";

void setup() {}

void loop() {}

Regards,
Ray L.

(deleted)

wtaulu:
Both using the extern and delcaring the .h after the global variable worked. Thanks.

Are both options considered "good practice" in arduino IDE?

Using extern is, arguably, better style, but I would use either.

Regards,
Ray L.

You should not be defining a function in an .h file. If that .h file gets #include(d) in more than one source file, you'll have 2 functions defined with the same name in your project. Since function names are global, the linker will be unhappy.

Function and variable definitions belong in .ino files (if you insist on using the Arduino multiple .ino file technique). Better yet, use proper .h / .cpp files and only have one .ino file.

EDIT:
Granted, what you're doing will work with multiple .ino files (as long as you have #include guards) since they'll all be mashed together for compilation anyway. It's just kind of kludgey (IMO).

gfvalvo:
You should not be defining a function in an .h file. If that .h file gets #include(d) in more than one source file, you'll have 2 functions defined with the same name in your project. Since function names are global, the linker will be unhappy.

NOT if the .h file has "include guards", which takes adding one whole line of code to the .h file....

gfvalvo:
Function and variable definitions belong in .ino files (if you insist on using the Arduino multiple .ino file technique). Better yet, use proper .h / .cpp files and only have one .ino file.

That is your personal preference, not the "law". Historically, c/c++ .h files have contained all kinds of stuff, including function definitions. There are often good reasons for doing this, and there is nothing wrong with doing it, where it makes sense.

Regards,
Ray L.

If you try to use a template function then the .h is the only place it will let you define it.

Delta_G:
If you try to use a template function then the .h is the only place it will let you define it.

True, I should have stipulated that. But, it's not applicable in this case. If (as written) the OP's .h file gets #included into more than one compilation unit in the same project, there WILL be multiple functions with the same name.

As I added in my EDIT, it won't happen if all the source files are .ino because of the way Arduino IDE mashes them together. But, (again IMO) the multiple .ino file technique is a poor substitute for using proper .h / .cpp files to create a multi-file project.

RayLivingston:
NOT if the .h file has "include guards", which takes adding one whole line of code to the .h file....

Include guards will not save you from linker errors. You still get multiple definition errors when a function definition is included in multiple translation units.

Imagine a structure like this:

Foo.cpp → Foo.h
Bar.h → Foo.h
sketch.ino → Bar.h
sketch.ino → Foo.h

Sketch includes Foo.h twice (once directly, and once through Bar.h), so you will need include guards in Foo.h to make sure the preprocessor includes it only once, otherwise, the compiler might complain.

The second problem is that if Foo.h defines a variable or function, it gets compiled twice (once for Foo.cpp, and once for sketch.ino). The compilation will succeed, because both Foo.cpp and sketch.ino are compiled separately. However, you will get an error during the linking stage, because there are now two definitions/implementation of the same variable/function available.

Pieter