About .INO and .CPP files
The difference between the two file types
Both file extensions, .INO and .CPP, relate to source files, there is no difference
in the file format or any difference in the programming language, but there are
differences in how Visual Micro handles the files during compilation.
Every project must have one main sketch file, which must be
an .INO file. This is the file containing the setup() and
loop() functions. This main .INO file must have the same name as
the project itself, e.g. if your project is named "MyRobot", then
your main .INO file must be called MyRobot.INO.
As long as you are using .INO files it does not matter how you distribute your
functions among the files and how they are ordered within them. This makes programming
easier, which is the idea behind .INO files.
However, as your project becomes larger and more complex, you might experience
certain limitations of the .INO approach, especially if you are starting to use
certain elements common to the C++ language like typdefs
or global variables. This will be the time when you should consider switching to
.CPP and .H files.
If you are using .INO files, you should follow these rules
to avoid problems with compilation:
1. Put all global variables and objects into the main .INO file (the one that has
the same name as your project)
If you want to use user defined data types like your own classes, structs or typedefs
as arguments to functions, then you must write function prototypes, like it
is common with .CPP files (Read more about function prototypes)
You can choose that .INO files shall behave just like .CPP files by switching the
setting in the Project Properties to "false".
.CPP and .H files
Even though the Arduino web site does not mention it, the programming language
Arduino uses is nothing but C++, with some limitations due to the tight memory constraints
of the processors used.
Every user that is familiar with C or C++ should use .CPP and .H files in all
but the simplest projects, because they show a behavior he is familiar with.
When using .CPP files, some "magic" of the .INO files does not apply. That's
why the user must care about typical C/C++ constructs like function prototypes.
At the end of the day, the larger the project gets, the more sense it makes to switch
to .CPP/.H files.
Which file type shall I use?
(This question is irrelevant as long as you have only one source file, which
must always be an .INO file.)
In a nutshell:
- If you don't want to learn about C++ declarations and header files, stay
with .INO files
- If you have a more complex sketch with many separate source files that use
classes and methods from each other, you will have more control and overview
if you use "classic" C++ .CPP and .H files
How Visual Micro handles the files during compilation
Before compilation, Visual Micro concatenates all .INO files in your project
in alphabetical order into one big .INO file. In this alphabetical order, all upper case letters are
preceding the lower case letters. Example: The files messages.INO, analog.INO,
and Digital.INO are concatenated into one singe file in this order:
Digital.INO, analog.INO, messages.INO
(note the upper/lower case in the file names).
Exception: The main sketch file is always the first in this order, regardless
of alphabetical order.
This way of handling .INO files might seem
somewhat counterintuitive, but Visual Micro tries its best to
mimic the way the original Arduino IDE does it, to ensure maximum
compatibility with existing projects when migrating from the original
IDE to Visual Micro.
To relieve the user from writing function prototypes, Visual Micro automatically
generates them for all functions in all .INO files and puts them at the head of
the combined .INO file.
After that combination step, the resulting, combined .INO file is compiled as
one large file.
.CPP are treated as with every common C++ compiler: They are compiled one after
another. The order how the files are compiled does not matter, because every .CPP
file is treated separately.
Visual Micro does not automatically create function prototypes for
.CPP files, so you must take care of them using header (.H) files, like it is common
What are function prototypes?
Read this Wikipedia page to learn about function prototypes.