correct way to include multiple files

hello,

since my code is getting fat i am willing to move some functions, defines and values in different files

example of what i would like to do:

main_file.ino:

void setup() {
byte version = VERSION;
}

void loop() {
function();
}

configuration file:

#define VERSION 11
//...

function file:

void function(){
//...
}

which is the correct way to do it?

edit: it wasn't as simple as it appear
i googled and i wan't able to find a solution
i also found an article from nick gammon Gammon Forum : Electronics : Microprocessors : How to avoid the quirks of the IDE sketch file pre-preprocessing
is any simple way to have the arduino ide use variables/void/ecc from other files?

The easiest way is to have multiple .ino files, but you previously said that won't work for you.

So the usual alternative is to add .h files, which contain declarations, and .cpp files, which contain definitions. #include the .h file in any other file where you want to use one of the functions declared in that .h file.

You can use .c files for your definitions. .c is the extension for C files but .ino files are turned into .cpp files before compilation. .cpp is the extension for C++. In order to use a C function in a C++ file, you need to wrap the function's declaration in extern "C" {}. That just adds one more complication so, at least to get started, I recommend to just use .cpp files instead of .c.

All the information you find on writing libraries will apply to your goal. This is a good introductory tutorial on writing a C++ library:

The only difference between .h/.cpp/.c/etc. files in your sketch folder vs the ones installed as a library is that you need to use a different #include syntax. When you use the #include <foo.h> syntax, only the standard libraries folders are searched for the file foo.h. If foo.h is in your sketch folder you will get a "file not found" error. You need to use the #include "foo.h" syntax, which will cause the local folder to be searched first, then the libraries folders.

Yep, it looks like i am making a simple thing complex

For now i am back at the arduino ide to avoid problems generated by vscode, this is the reason why i edited the first post

So if the arduino ide merges all .ino fine in a single .cpp file do you think that having a single .ino file + some .cpp file will work anyway?
Or as you said do i need to make the pairs h/cpp as it where a library?

If you use multiple .ino files you need to be aware that the compiler first loads the principal file (the one with the same name as the folder) and then loads the others in alphabetical order.

Variables defined in an earlier file will be visible to code in a file that is loaded later, but not vice-versa.

For my own projects I just put the code into .h files and I never bother with .cpp files.

...R

Robin2:
If you use multiple .ino files you need to be aware that the compiler first loads the principal file (the one with the same name as the folder) and then loads the others in alphabetical order.

Variables defined in an earlier file will be visible to code in a file that is loaded later, but not vice-versa.

...

I am aware of this since yesterday, and i don t like this behaviour, this is one of the reason why i would like to have just one .ino file

Robin2:
....

For my own projects I just put the code into .h files and I never bother with .cpp files.

...R

Do you mean that you have only one .ino file and then one/some .h, right?

How does the compiler behave in this situation? Does your .h file see what is defined in the others or not?

I am willing to adopt the technique programmers with experience do

aster94:
I am willing to adopt the technique programmers with experience do

I do not consider myself an example of good C++ programming style. I am a reluctant user of C++ so i just use the simplest system that works for me.

For some projects I have most of my code in a series of .h files each of which is #included by the .INO file.

And some of the projects involve separate Arduinos (and thus different .INO files) which must share some of the .h files.

The real PITA of the Arduino system is its inability to #include files referred to be a relative path name. Either the .h file must be in the same folder as the .INO file or else you must use a full path name to refer to it. To get around that I have written a short Python program to compile and upload programs using the command line Arduino IDE.

...R

the .ino/.h system worked for me in my most complex project (which is only 3 files ahahah) i didn't had any problem so i am pretty sure that if pert doesn't have a better system i will adopt this even if i know that having the couple h/cpp is better also for portability

the only problem for now is that if in a .h file i want to call a function from the same file i have to put it at the beginning

karma+ for both!

aster94:
the only problem for now is that if in a .h file i want to call a function from the same file i have to put it at the beginning

Putting a function prototype at the top of the .h file deals with that.

In fact the need for function prototypes is standard C++ behaviour and the Arduino IDE creates them from regular .INO files before it passes the file to the compiler.

...R

Yep i know, last year i used atmel studio to try bare metal coding on avr and I lost the first 10 minutes for this reason

I like automatic prototype generation so I only use .ino files in my sketches. When I have code I want to share between sketches I put it in a library. Some of these have turned into header only libraries over time. There are two reasons for this:

  • It allows me to configure the library code using macros defined in the sketch.
  • For certain applications templates are super useful but the code of a templated function must be in the header file. It's great to have a nice clean header file with declarations and a .cpp file with all the definitions but when I start ending up with some code in the .cpp, some in the .h, and needing to search through both files to find it that benefit is lost and it's easier for me to have everything in one file.

My understanding is that putting the code in the header file can increase compile time since the code is compiled for every time it's included but how significant of an issue this is depends on how many times it's included and how much code there is.