Replies: 5 comments 8 replies
-
I think it should just work, if the other tabs are also .ino files. Apparently, the compiler concatenates all .ino files, starting with the one that matches the folder/project name, and compiles them as though they were one big long file. So, anything declared in the main one should be visible to the others, though not the other way around - you'd need to declare function prototypes in the main .ino, to be able to use functions declared in the others, for example. [EDIT: It seems you may only have to do that for functions with default values, i.e. optional parameters. The IDE should generate the rest automatically. See https://forum.arduino.cc/t/default-argument/885323/6] There are other approaches, like using #include "another_tab.h", to just insert another chunk of the program into the main tab -. h files don't have to be header files, as such, they're just inserted where the #include statement is. You can also make the other tabs as .cpp files, and use extern to refer to things in them. This thread covers some of that, but maybe not all the details: https://forum.arduino.cc/t/how-to-properly-include-functions-written-on-other-sketch-tabs/565672 |
Beta Was this translation helpful? Give feedback.
-
put
into globals.h |
Beta Was this translation helpful? Give feedback.
-
Not a complete answer, but I think you need to swap the order of these two in globals.cpp :
Also, globals.h has the extern declarations (i.e. defining the references as being external), so you don't want that part included in the .ino, where those two things are defined. In C++ you only do that for const definitions, apparently. You could either split globals.h into two parts, or use a #define to say this is the file where they're actually defined, and a #ifndef ... #endif in globals.h, to stop it doing that part when it's included in the .ino. Also, unless I'm missing something, you need to uncomment this line, so you're actually defining the usb_midi object in the .ino file:
|
Beta Was this translation helpful? Give feedback.
-
Here's a couple of references on how extern is used in C++, which should also apply here: https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c https://docs.microsoft.com/en-us/cpp/cpp/extern-cpp?view=msvc-170 Basically, in C++ you use extern in any module where you want to refer to a variable that's in another module (that's called a declaration) but you can't use it in the module where the variable is defined... unless it's a const, for some reason. The rule is different in C, which lets you have extern (as a declaration) followed by a definition of the variable - so you'd get to use the same include file in each module. |
Beta Was this translation helpful? Give feedback.
-
No problem. I had success converting a multi module C program to Arduino a while ago, by leaving most of it as C and using extern "C" ... in the ,ino main module to refer to global variables and functions in the C modules. This is a little different though because you're using the C++ library. I'd be tempted to go with one big .ino too, and make use of the folding editor functions of the IDE, or break it up into multiple .ino modules using the rules explained in one of the first links I gave, in an earlier post - the compiler just pastes them all together, starting with the one matching the folder name, then apparently in alphabetical order. That might be more convenient than the one big .ino approach, but it amounts to the same thing, really. There would be no difference in compile time, for one thing. |
Beta Was this translation helpful? Give feedback.
-
I use this statement at the top of my main file (with setup() Loop()):
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);
How do I make the "MIDI" global, so I can use it in other files?
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions