#include in multiple sketches

I'm breaking my project into multiple .ino files to make it a bit more readable since it's my understanding that they are all combined upon compile. I just wanted to check on the usage of #include <library.h>.

Do I include all libraries in every sketch? Or just the sketches in which they are used? Or can I get away with only including them in the main sketch.

Thanks

once, in main.

1 Like

you need to include the "#include " in any file that references one of the symbols (functions, variables) in the library. your loop() will probably call functions in your separated files.

if you have a function, abc() in abc.cpp (or abc.ino), then abc.h will have a declaration, void abc (); and will be included in the .ino file with loop(), #include "abc.h

as you said, along with your multiple files, the library files, the .cpp files, are copied along with your sketch files, the .ino and .cpp, files into the build directory and built to create the binary downloaded and executed on the Arduino.

The .ino file concatenation order is the same as it has always been:

https://arduino.github.io/arduino-cli/latest/sketch-build-process/#pre-processing

All .ino and .pde files in the sketch folder (shown in the Arduino IDE as tabs with no extension) are concatenated together, starting with the file that matches the folder name followed by the others in alphabetical order.

I went looking for this nugget when I started breaking up my main ino file; dug for quite a while in the documentation suite, couldn't locate it(obviously, my dictionary of magic words is deficient). So I simply set up a simple suite of 3 .ino files, one of which(fortunately) was the original one, same name as it's folder; it didn't take long to figure out what order worked, and what didn't. Has worked seamlessly since then. Glad to see my findings are validated.

It is more correct to break the code not just to make it "readable", but according to the tasks used. For example, all the code that works with TFT screen - in a first file, with motors - in a second, with the Internet - in the third.
Then it will be obvious that the display library should be included in the first file, and the network functions library - in the third one....

various shortcuts can be taken in the Arduino environment compared to a conventional build environment.

The one thing that does not work reliably is function prototypes.

So I want to organise my stuff in multiple inos

MultipleIno2.01.cpp is the main sketch file and contains all variables, definitions and so on

struct POSITION
{
  int x;
  int y;
};

POSITION pos = {0, 0};

and separate files for setup(), loop() and the functions.

a_setup.ino

void setup()
{
  Serial.begin(115200);
  while (!Serial) {}
  // CORRECTION; thanks @ptillisch
  // printPos(pos);
  printPosition(pos);
}

b_loop.ino

void loop()
{
  // put your main code here, to run repeatedly:
}

c_functions.ino contains all functions

void printPosition(POSITION p)
{
  Serial.print(F("p.x = "));
  Serial.println(p.x);
  Serial.print(F("p.y = "));
  Serial.println(p.y);
}

Below is the (corrected) file that is created by the Arduino builder

#include <Arduino.h>
#line 1 "C:\\Users\\Wim\\Documents\\Arduino\\Forums\\_DemoCode\\MultipleIno2\\MultipleIno2.0.1\\a_setup.ino"
void setup();
#line 1 "C:\\Users\\Wim\\Documents\\Arduino\\Forums\\_DemoCode\\MultipleIno2\\MultipleIno2.0.1\\b_loop.ino"
void loop();
#line 1 "C:\\Users\\Wim\\Documents\\Arduino\\Forums\\_DemoCode\\MultipleIno2\\MultipleIno2.0.1\\c_functions.ino"
void printPosition(POSITION p);
#line 0 "C:\\Users\\Wim\\Documents\\Arduino\\Forums\\_DemoCode\\MultipleIno2\\MultipleIno2.0.1\\a_setup.ino"
#line 1 "C:\\Users\\Wim\\Documents\\Arduino\\Forums\\_DemoCode\\MultipleIno2\\MultipleIno2.0.1\\MultipleIno2.0.1.ino"
// C:\Users\Wim\AppData\Local\Temp\arduino\sketches\4C00CB8A892D58E1DEFAE9736B98376F

struct POSITION
{
  int x;
  int y;
};

POSITION pos = {0, 0};





#line 1 "C:\\Users\\Wim\\Documents\\Arduino\\Forums\\_DemoCode\\MultipleIno2\\MultipleIno2.0.1\\a_setup.ino"
void setup()
{
  Serial.begin(115200);
  while (!Serial) {}
  printPosition(pos);
}

#line 1 "C:\\Users\\Wim\\Documents\\Arduino\\Forums\\_DemoCode\\MultipleIno2\\MultipleIno2.0.1\\b_loop.ino"
void loop()
{
  // put your main code here, to run repeatedly:
}

#line 1 "C:\\Users\\Wim\\Documents\\Arduino\\Forums\\_DemoCode\\MultipleIno2\\MultipleIno2.0.1\\c_functions.ino"
void printPosition(POSITION p)
{
  Serial.print(F("p.x = "));
  Serial.println(p.x);
  Serial.print(F("p.y = "));
  Serial.println(p.y);
}

And if that is compiled you will get errors; as a newbie I will probably throw the towel in the ring and work with one massive ino file. This is a known issue for years and still has not been resolved (I don't say that it will be easy to resolve).

Another problem for newbies can be that if there is an error in one of the ino files, the compiler might very well complain about another ino file that was not modified.

Not related to multiple inos but a similar problem: Incorrect placement of auto-generated prototypes gives misleading error messages · Issue #362 · arduino/arduino-builder · GitHub. That was with IDE 1.8.13, just tested it with IDE 2.0.1 and the issue is still there.

Note:
personally I use .h and .cpp files but to make things easier for difficult customers I sometimes use multiple inos and it bites me every time.

And you'll get the exact same error even with a single .ino file. The use of multiple .ino files is completely irrelevant to this problem.

I was mislead by this error in your code, which will cause the sketch to fail to compile regardless. I can reproduce the prototype generation failure that is specific to the multiple .ino files after correcting that error:

printPosition(pos);

Thanks for waking me up :slight_smile:

Indeed, sorry for that. I have fixed the original post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.