Multiple .ino files vs. .h and .cpp


(Scott) #1

Continuing the discussion from [WIP] Arduzilla! - Kaiju City Destruction Game:

This is a discussion that was going off topic in the above topic, moved here in case of further interest.


[v0.2] [WIP] Arduzilla! - Kaiju City Destruction Game
(Scott) #2

Not only is it legal, it’s the expected way of doing things in the Arduino environment.


[v0.2] [WIP] Arduzilla! - Kaiju City Destruction Game
(Pharap) #3

I can’t find anything to support this claim.

Most of the (mostly unofficial) websites I’ve found recommend using .h and .cpp files.
(E.g. visualmicro, arduino land)


(Scott) #4

Let me turn it around and ask and point out:

  • Can you find any official Arduino documentation with examples or recommendations for using .cpp or .h files (for sketches, not libraries).

  • Why would Arduino invent the .ino format in the first place if it recommended the use of .h and .cpp?

  • If multiple .ino files weren’t expected, why does the build process pre-processing concatenate all .ino files into a single file (which it then turns into a .cpp file)?

  • Why does the IDE make it simpler to create and use multiple .ino files? If you create a new tab in the IDE and name it without an extension, it automatically adds the .ino extension. The IDE also hides the IDE extension, by default (I presume because there’s no need to know about it if that’s the only type of file used).

  • In the Arduino Software (IDE) document’s Tabs, Multiple Files, and Compilation section, it states:

    These can be normal Arduino code files (no visible extension), C files (.c extension), C++ files (.cpp), or header files (.h).

    The word normal, at least to me, implies the type of file that you would normally use.

  • Arduino’s documentation for the #include statement says:

    #include is used to include outside libraries in your sketch.

    It makes no mention of using it to include local sketch files, of any type.

IMHO the Arduino environment was created to make it easy for “makers” to get their project working simply and quickly, using standardized hardware and software that makes it easy to do simple things without having to learn some of the more complicated aspects of using C++. It’s not intended to be a platform to learn C++ programming (at least primarily).

Standard .cpp and .h files are supported for cases when a sketch becomes too complex for the capabilities of the .ino pre-processor, for experienced programmers, or to make it easier to port programs from other environments. This is true for some Arduboy sketches, but for many others, simply using only one or more .ino files would work fine.