Question about the Arduino/libraries file structure

Hi, I'm new to Arduino programming, and I feel so excited to participate in this community. In particular, I'm very interested in learn more about the platform, as many other boards can be programmed using Arduino IDE (raspberry, beagle bone, up, etc). However, I'm a little concerned about the implications of using Arduino IDE to program on other boards. I've read in stackoverflow that some developers complained about Arduino IDE adding code on the background, to ease work of developers. For example, including "arduino.h" is not required when writing a new *.ino file, but it is if you are writing a library. So I wonder what is the impact of these IDE actions when writing programs for other boards. Is the code of arduino.h added even if it is not used on other boards?

Another thing that I do not understand is why some of the "Core" libraries of the framework are located on the WiFi directory structure? And also duplicated (for example compiler.h which is located in C:\Program Files (x86)\Arduino\libraries\WiFi\extras\wifi_dnld\src\SOFTWARE_FRAMEWORK\UTILS) and in wifiHD...UTILS?

Some commonly-used constants like HIGH, or LOW are defined in "compiler.h". But, declaring them in wifi* directory does not make sense to me, if we accept that we can create a project on a board that does not contain a wiFi module, so why to create that dependency?

Thanks for your advice, and I appreciate if you could refer me to some documentation is there is on this subject.

Kind regards,

Rodrigo.

rodrigomorales:
I've read in stackoverflow that some developers complained about Arduino IDE

I've found that frequently when you see some experienced developer complaining about Arduino they actually have no clue what they're talking about because either their entire knowledge of of Arduino is based on other "developers" complaining about it or they spent five minutes looking at it back in 2011. I advise you to be skeptical of information that comes from people who don't actually have any experience with Arduino.

rodrigomorales:
adding code on the background, to ease work of developers.

Actually the purpose of Arduino is to make it easy for beginners, who may have little or no prior experience in programming and electronics to use microcontrollers. The things done to that purpose may not be desirable to someone who is already experienced in these subjects or even a beginner on the track to working in embedded systems as a career. There are other excellent options available for those people.

rodrigomorales:
For example, including "arduino.h" is not required when writing a new *.ino file, but it is if you are writing a library.

The Arduino IDE does a few things automatically to all .ino files in the sketch before renaming them with the .cpp (C++) extension and compiling them:

  • Concatenate all .ino files, starting with the file that matches the folder name, then the rest in alphabetical order.
  • Add #include <Arduino.h> if that line does not already exist.
  • Generate function prototypes for all functions that don't already have a prototype.
  • Add #line directives to make any warning/error messages generated by the resulting code still match the user's .ino files.

The Arduino IDE does not do any preprocessing of sketch files with extensions other than .ino. You can use .h, .cpp, .c, .hh, .hpp, .S files in your sketch if you like.

rodrigomorales:
So I wonder what is the impact of these IDE actions when writing programs for other boards.

Well, I'm not sure what you mean by "impact", I guess you can infer it yourself from what I said above or be more specific and I'll try to answer.

rodrigomorales:
Is the code of arduino.h added even if it is not used on other boards?

The Arduino.h include is always added to the .ino file unless it already exists but when you speak of "other boards", you should understand that each hardware package provides its own core library, which contains the Arduino.h file. The hardware package associated with the board you have selected in the Tools > Board menu is used during compilation. There is not one universal Arduino.h file used for all boards.

If you don't want Arduino.h included here's the way to accomplish that. Just add the following lines to your sketch:

#if false
#include <Arduino.h>
#endif

The sketch preprocessor sees that the line #include Arduino.h is already in the sketch and so doesn't add it. Now you'll find that all that Arduino core library magic to help beginners is gone. You can just have those three lines in your sketch and add files with regular .h/.c/.cpp extensions to program in regular old C, C++, assembly. Don't forget to define your main() function because the one defined in the Arduino core library will no longer be used.

rodrigomorales:
Another thing that I do not understand is why some of the "Core" libraries of the framework are located on the WiFi directory structure? And also duplicated (for example compiler.h which is located in C:\Program Files (x86)\Arduino\libraries\WiFi\extras\wifi_dnld\src\SOFTWARE_FRAMEWORK\UTILS) and in wifiHD...UTILS?

Some commonly-used constants like HIGH, or LOW are defined in "compiler.h". But, declaring them in wifi* directory does not make sense to me, if we accept that we can create a project on a board that does not contain a wiFi module, so why to create that dependency

If you have a look at the Arduino library specification:

The content of the extras folder is totally ignored by the IDE; you are free to put anything inside such as supporting documentation, etc.

I believe those files you see are for the firmware on the WiFi chip used on the Arduino WiFi shield, not for actually programming an Arduino board. That is not a dependency.

@pert, that is a wonderfully comprehensive Reply. I have bookmarked it. I hope you don't mind if I refer others to it.

...R

Not at all, I'm glad if it can be useful to others. There's a good feeling to providing one-on-one help but it's not terribly efficient. I like to think that some of the forum posts might help many others in the decades to come and thus provide a multiplier effect to our efforts.

I thought the trick of disabling the Arduino.h include was pretty neat when I learned about it. They specifically added that because someone complained about the automatic addition of the Arduino.h include on the issue tracker. I don't mind the preprocessing the Arduino IDE does to sketches but I do want the ability to control it just in case I ever want to do without.

@Pert. Thanks for your comprehensive answer. I did not knew that extras folders was totally ignored. Still, I wonder if the code of the arduino.h and its corresponding arudino.cpp source code. I ran grep to search for these files on the arduino IDE directory and throw me the two locations (the extras folders that I mentioned before). I also run some statistical analysis tools like Sci Understand and all show several dependencies to ../Arduino/libraries/WiFi/ folder. So where is the correct location of the arduino.h and arduino.cpp. I'm very interested to know what is defined in this library.

Kind regards,

Rodrigo.

rodrigomorales:
I also run some statistical analysis tools like Sci Understand and all show several dependencies to ../Arduino/libraries/WiFi/ folder.

You're being quite vague. That's not productive when discussing complex technical topics. Unless you want me to give you a vague, useless answer you'll need to provide specific details.

rodrigomorales:
So where is the correct location of the arduino.h

Do you mean Arduino.h? As I explained previously:

pert:
you should understand that each hardware package provides its own core library, which contains the Arduino.h file. The hardware package associated with the board you have selected in the Tools > Board menu is used during compilation. There is not one universal Arduino.h file used for all boards.

The easiest way to find the active hardware package location is as follows:

  • Select a board from the hardware package from the Tools > Board menu
  • File > Examples > SPI > BarometricPressureSensor
  • Sketch > Show Sketch Folder
  • Move up folder levels until you reach the one that contains boards.txt

You will then find Arduino.h in the cores/{core name}/ subfolder.

rodrigomorales:
and arduino.cpp

I've never seen a hardware package that had a file named Arduino.cpp. You will find many other source files in the core library folder though.