If I have functions or portions of code that I want to put into a file that can be used in any Arduino sketch or referenced by any arduino code, where should I go to learn about how to do this? Is this the same as making a library?
Pretty much. See My Post #5 in this Thread for some basic guidelines to partition the code between header and source files.
I found this to be a very helpful tutorial when I was starting with Arduino:
A H-file declares (does not define) macros, external references, function (prototypes).
A C-file is the implementation of code, functions and definition of variables
Any other file can just use (#include) the H-file and gets a clue what the interface of the code file is.
A library is a bit different:
A LIB is an almost "self-contained" pre-compiled set of H and C-files. Later, the linker binds code with binary in order to resolve external references, which is in LIB file.
A LIB file, even you would have all the H- and C-files will not be changes when you modify stuff in those files (belonging to the LIB). A LIB has to be built with a separate makefile, compile process. A LIB is not updated or rebuilt when you change project files using a LIB.
It saves compile time.
It is always a good idea to split code into H-files and C-files.
Rules are: H-files do not contain any code, not any definition (e.g. creating variables), just declarations (extern, references, prototypes, macros).
The H-file is the "documentation" of your C-file - the interface to use the C-file.
sorry: defines macros, but does not define variables (any stuff allocating memory)
That may be correct for one meaning of "library", but that is not accurate for what we mean when we say "library" in the Arduino world.
An Arduino library is simply a way of sharing reusable code between multiple projects. It is possible (though rare) to precompile them, but it is in no way required to call something an Arduino library.
As noted the vast majority of libraries in the Arduino ecosystem are not pre-compiled but simply source code. The only major exception that I know of are the native APIs that come with the ESP8266 and ESP32 processors. This includes FreeRTOS in the case of ESP32.
There are some precompiled standalone Arduino libraries. I found a couple of examples of official ones after a quick browse:
https://github.com/arduino-libraries/Arduino_Braccio_plusplus
I believe this one is precompiled to reduce sketch compilation time for the huge lvgl library used for the display on the Braccio++.
https://github.com/arduino-libraries/Arduino_MCHPTouch
I believe this one is precompiled because Microchip doesn't share the source code (Arduino only wrote a wrapper around the binary Microchip distributes).
The exceptions that prove the rule.
Exactly. As I said from the start, these are very rare.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.