Arduino IDE Version Requirement For Multiple ino Files

Hello,

I am aware that I can have multiple .ino files in a project folder, but I am wondering what is the minimum requirement for the Arduino IDE version to compile multiple .ino files without running into issues about which .ino files are compiled first?

Ex) if my main file: project.ino and another file was zebra.ino, I remember I had to update my Arduino IDE to a newer version because I ran into issues due to the functions written in zebra.ino not being recognized since zebra.ino was compiled after project.ino was compiled and functions in zebra.ino were called inside project.ino

I couldn't find the information about the minimum IDE version requirement for this. Thank you for your help!

You don't mention which IDE version you are currently using, but, .ino files are concatenated prior to being compiled. First will come the file that has the same name as the project folder. After that are added all the other .ino files in alphabetical order. You can determine in which order the files are added by using an alphabetical naming scheme.

You can avoid the ordering problem entirely by using .h and .c or preferably .cpp file pairs for your additional files. Suppose you had a group of functions, you would put them into a pair of files called myfunctions.h and myfunctions.cpp. There are rules regarding how these files are structured and we are getting into the realms of C/C++ here, but this is not difficult. The .h file is called a header and contains your function declarations and also requires something called an "include guard" which looks like this at the top of the .h file:

#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H

You then have your global (external) variable and function declarations.
At the bottom off the .h file you also must have:

#endif  // MYFUNCTIONS_H

Not that the part after the '//' is a comment and not mandatory, but it does help to identify the #if statement that it is paired with.

At the top of myfunctions.cpp you will have:

#include Arduino.h
#include myfunctions.h

Your variable definitions and code then follow.

In C/C++ a variable or function has to be declared before it can be used and as we can see, such declarations would be placed in header the (.h) file. The .c or .cpp file would contain the variable and function definitions (i.e. the working code). The compiler "knows" about the functions because it looks at the declarations in the .h file which is included in every file where those functions are used. Your corresponding .cpp will include the .h file and most likely so will your main sketch .ino file. You include with a simple statement:

#include "myfunctions.h"

Note the double-quotes rather than the <> characters. This signals to the compiler to look for the file in your project directory rather than the Arduino libraries. You add .h and .cpp files to the project using tabs in IDE as you have been doing, just give them the appropriate extension when you name them.

With .ino files, Arduino IDE tries to make things easier so usually no forward declarations are necessary. This is fine until you start using multiple .ino files as you are doing. There is nothing to stop you putting forward declarations of the global variables and functions used in the secondary .ino file in the declaration section at the beginning of the primary .ino, i.e. before void setup(). A forward declaration of a function consists of the function statement only, so say you have some function in file2.ino:

void myFunction(int foo){
 some code here
}

Your forward declaration in the primary .ino would simply look like:

void myFunction(int foo);

That way when the compiler encounters a reference to a function or global variable before the appropriate .ino file containing it is loaded, it already "knows" about it as it has a reference to it and will link to the working code at some point in the process without throwing an error. However if you have a lot of .ino files, this can get messy very quickly so for complex projects, using .h and .cpp files is recommended.

Thank you for the reply. I was hoping to just use multip .ino files because I am working on a project that is geared towards making it accessible to people with not a lot of technical background. I figured properly designing libraries and for them to have to install them correctly later on would be harder and some people working on the project have already experienced some difficulty with this. I am currently using 1.8.10 but I remember the version from 2016 (I don't remember the exact version) was unable to recognize the functions defined in other .ino files.

2016 is long ago; things have evolved. But might not be perfect yet.

Most versions would do; even 1.6.6 if I recall correctly. But you can happily force contributors to a certain version.

The Arduino builder at occasion can drop some stitches resulting in functions not found (maybe even variables, can't remember); as I don't use multiple ino files (but indeed the .h/.cpp approach), I'm not sure how the latest IDEs behave.

A good habit with multiple ino files is to at least have all function prototypes in the main ino file. For variables, it might prevent issues if you add them as extern in the main ino file or declare them in the main ino file and declare them extern in the other ino files; the latter is probably preferred.

You don't have to use libraries with the .h/.cpp approach; you can give others the full directory (including the .h/.cpp files located in your sketch directory) and they will have everything they need; just need to include them with double quotes instead of < and >.

I see. Thank you so much!