Include #include <Arduino.h> framework useless

Hello everyone,

I got a very basic question. I am using arduino Framework.
I got a #include <Arduino.h> in the skeletton of the main.cpp program.

What is the role of this include ?
When I remove it, the program is still building. So is it useless because in platformio.ini we already write that we are using arduino framework ?

I check framework documentation and I haven’t found infos about that.

Imagine a project with many .cpp files. Some of these C++ files might use the Arduino functions such as digitalWrite() while others might not use them. By not including Arduino.h, they compile faster and are less likely to have name conflicts.

Thus, with the #include statements, you control at a file level if the Arduino functions are used while the framework declaration in platformio.ini controls at a project-wide level how the program is built and if the Arduino header file is available or not.

You are saying that your project still builds even if you remove the #include statement form main.cpp. This is likely because Arduino.h is included indirectly by another header file that main.cpp includes.

2 Likes

Thanks for your answer.
When I look at my dependency graph I can see that I am using arduino function but not directly arduino.h.

A part of my question is what should include ?
Do I need to put an include for all the sub dependency in my main.cpp or should I let platformio found them in my custom lib folder ?

I am currently putting only main dependency in my main.cpp but I want to be sure that it won’t have side effect that i didn’t thought about.

Dependency Graph
|-- <Adafruit TSL2591 Library> 1.3.1 (C:\Users\clem\Documents\Code\platform\lib\Adafruit_TSL2591)        
|      |-- <Adafruit Unified Sensor> 1.1.4 (C:\Users\clem\Documents\Code\platform\lib\AdafruitUnifiedSensor)
|      |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|-- <BoardDefinitionV3_1> (C:\Users\clem\Documents\Code\platform\lib\BoardDefinitionV3_1)
|-- <BoardSetup> (C:\Users\clem\Documents\Code\platform\lib\BoardSetup)
|     |-- <BoardDefinitionV3_1> (C:\Users\clem\Documents\Code\platform\lib\BoardDefinitionV3_1)
|-- <KZB_rtc> (C:\Users\clem\Documents\Code\platform\lib\KZB_rtc)
|     |-- <Low-Power> 1.6.0 (C:\Users\clem\Documents\Code\platform\lib\Low-Power)
|     |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|-- <protocol_RF> (C:\Users\clem\Documents\Code\platform\lib\protocol_RF)
|     |-- <arduinoUtils> (C:\Users\clem\Documents\Code\platform\lib\arduinoUtils)
|     |     |-- <arduinoMultiprotocol> (C:\Users\clem\Documents\Code\platform\lib\arduinoMultiprotocol)
|     |     |     |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|     |     |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|-- <Adafruit Unified Sensor> 1.1.4 (C:\Users\clem\Documents\Code\platform\lib\AdafruitUnifiedSensor)
|-- <arduinoSigfox> (C:\Users\clem\Documents\Code\platform\lib\arduinoSigfox)
|     |-- <arduinoUART> (C:\Users\clem\Documents\Code\platform\lib\arduinoUART)
|     |   |-- <arduinoMultiprotocol> (C:\Users\clem\Documents\Code\platform\lib\arduinoMultiprotocol)
|     |   |   |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|     |   |-- <arduinoUtils> (C:\Users\clem\Documents\Code\platform\lib\arduinoUtils)
|     |   |   |-- <arduinoMultiprotocol> (C:\Users\clem\Documents\Code\platform\lib\arduinoMultiprotocol)
|     |   |   |   |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|     |   |   |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|     |-- <arduinoUtils> (C:\Users\clem\Documents\Code\platform\lib\arduinoUtils)
|     |   |-- <arduinoMultiprotocol> (C:\Users\clem\Documents\Code\platform\lib\arduinoMultiprotocol)
|     |   |   |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|     |   |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|-- <arduinoUART> (C:\Users\clem\Documents\Code\platform\lib\arduinoUART)
|     |-- <arduinoMultiprotocol> (C:\Users\clem\Documents\Code\platform\lib\arduinoMultiprotocol)
|     |   |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|     |-- <arduinoUtils> (C:\Users\clem\Documents\Code\platform\lib\arduinoUtils)
|     |   |-- <arduinoMultiprotocol> (C:\Users\clem\Documents\Code\platform\lib\arduinoMultiprotocol)
|     |   |   |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|     |   |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|-- <arduinoUtils> (C:\Users\clem\Documents\Code\platform\lib\arduinoUtils)
|     |-- <arduinoMultiprotocol> (C:\Users\clem\Documents\Code\platform\lib\arduinoMultiprotocol)
|     |   |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|     |-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)
|-- <DallasTemperature> (C:\Users\clem\Documents\Code\platform\lib\DallasTemperature)
|     |-- <OneWire> (C:\Users\clem\Documents\Code\platform\lib\OneWire)
|-- <OneWire> (C:\Users\clem\Documents\Code\platform\lib\OneWire)
|-- <Wire> 1.0 (C:\Users\clem\Documents\Code\platform\lib\Wire)

The dependency graph shows the dependencies between libraries and doesn’t really tell anything about the included header files and the used functions.

As a rule of thumb:

  • Include a header file for the classes and functions you are using in that particular .cpp file.
  • Do not include header file for things that might be used by a library but not by the particular .cpp file. This will happen automatically.

That way you should be safe even if the implementation of a library changes and it adds or remove dependencies and header files.

In some cases, you might need to include additional header files as they have an important side effect, e.g. adding Wire.h to enable I2C communication for a sensor that supports several protocols.

2 Likes

It isn’t useless, it is simply happy coincidence that it is working for you. I know for a fact the Adafruit libraries have #include <Arduino.h> in them, so it’s just luck that it being done already in a library like that is making it work for you.

As soon as you use any of the actual Arduino functions like pinMode(), digitalWrite(), delay(), and the setup and loop functions, you are using Arduino functions, and really should have #include <Arduino.h> at the top of your main cpp file.

Try a simple example, the classic blink sketch. If you don’t add #include <Arduino.h> to the top of your main.cpp, I think you’ll find it doesn’t work. Just because you’ve said that you are using the Arduino framework, it doesn’t mean that the compiler is going to detect that you need the functions and variables declared via Arduino.h… it simply tells PlatformIO what compilers to use, and how to compile the files.

Be kind to your future self - if you use commands from other files and libraries, add the includes for them… don’t rely on a particular version of a particular library to coincidentally do it for you, and breaking in the future if something else changes :wink:

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}
2 Likes

Thanks for your answer and the examples @pfeerick and @manuelbl

I read again the platformio documentation about dependency finder and I think my graph doesn’t show everyting because the mode is on chain and not deep.
https://docs.platformio.org/en/latest//librarymanager/ldf.html#dependency-finder-mode

1 Like