Ino (Arduino) and CPP file differences - For Peter

Tell us about your other hobbies & none QL related items here :)
User avatar
NormanDunbar
Forum Moderator
Posts: 1493
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Ino (Arduino) and CPP file differences - For Peter

Postby NormanDunbar » Fri Jan 29, 2021 11:12 am

I'm writing this here as it's off topic and I don;t want to hijack the QLUB Adapter topic where this started.

Back in December 2020, Peter asked in the QLUB Adapter topic (viewtopic.php?f=2&t=3590) "Is there any difference between .ino file format and and C language?"

I wrote this up in my book, which I won't plug here, but basically, there is no diifference in the content of the ino files, just how they are handled by the compilation system used by the Arduino.

An ino file contains C and/or C++ language structures and code. It must live in a file named 'xxxx.ino' and that file must live in a folder named 'xxxx'. Other ino files, as well as h, hpp, c or cpp files can live in the same folder. Opening such a project in the Arduino IDE results in the ino with the name of the folder being opened as the main file, and all the other ino, h, c, hpp and cpp files being opened on separate tabs in the editor.

So, basically, you write your code in C/C++ using as many ino, c, cpp, h or hpp files as you feel necessary. However when doing so, you never need to bother about including the required header files or similar, except in very unusual circumstances which I might exlain below, perhaps!

For example, if in an ino file, you want to use a data type of 'uint32_t' (unsigned int 32 bits long) then you just do so and don't bother about remembering to '#include <stdint.h>' because at compilation time, things happen to make this unnecessary. No problems will arise if you do include the header file though.

This feature doesn't apply to any *.c or *.cpp files though, they must adhere to corrrect C/C++ rules and regulations. Headers must be included and such like.

So, at compile time, what happens? The first stage is the preprocessor:

1. If necessary, a temp folder is created in wherever your user's temp stuff goes. This will be reused for as long as the project is open in the IDE. This folder will be named $TMP henceforth.

2. The main xxxx.ino file is copied to $TMP/xxxx.ino.cpp.

3. Any remaining ino files are appended to $TMP/xxxx.ino.cpp in alphabetical order, additional *.c, *.cpp and *.S files are copied to $TMP unchanged.

4. '#include <Arduino.h>' is prepended to the start of $TMP/xxxx.ino.cpp.

5. Any libraries used in the sketch are detected and the paths to their includeed header files determined. This is done using a temporary run of gcc/g++ with a certain setting to determine dependencies. A dependency file, *.dep, is written to $TMP (somewhere which slips my mind!)

6. Any functions remaining which have not yet been found a prototype are prototyped somehow, by the preprocessor, and if any remain "unfound" afterwards, cause an error and the developer must add them to the main sketch (or another ino) manually, and recompile.

The next step is the compiler:

7. The $TMP/xxxx.ino.cpp is compiled to $TMP/sketch/xxxx.ino.cpp.o.

8. Any *.c or *.cpp files are compiled to $TMP/sketch/*.c.o (or *.cpp.o).

9. In later editions of the IDE, any *.S (Assembly) files are assembled to $TMP/sketch/xxxx.S.o.

10. Libraries are compiled to $TMP/libraries/*.xxx.o -- xxx is the original extension, c or cpp. These are then 'ar' processed to make a static library, libXXXX.a where XXXX is the library name.

11. The Arduino core files are then compiled together to $TMP/core/*.xxx.o and then 'ar' processed to make $TMP/core/libcore.a which is another static library.

12. The various *.xxx.o files in $TMP/sketch are linked with the static libraries in $TMP/libraries and with the core in $TMP/core/libcore.a to generate $TMP/xxxx.elf

13. The elf file is processed to extract any required EEPROM data. This creates a file named $TMP/sketch/xxxx.eep. This is a bit of a waste of time as the Arduino IDE cannot upload the eep file using a bootloader.

It can, however, with an ICSP device, like usbTiny, but using one of those defaults to wiping the entire ATmega328p (chip erase), including the EEPROM beforehand. This means that every upload of a sketch, using EEPROM or not, performed with an ICSP will wipe the EEPROM and reduce its guaranteed 10,000 write cycles per cell.

If you use a programmer, you need to make sure you edit programmers.locaal.txt (create it if necessary) and add on the extra parameters to prevent your device wiping the whole chip, for me that's '-D' for usbTiny. However, that change makes uploading further EEPROM data "interesting" to say the least and you have to undo the "don't wipe the chip" setting before uploading any new EEPROM data.

If you have used something like avrdude to upload an eep file, the Arduino bootloader can happily upload a new sketch and will not affect the EEPROM data. Useful perhaps?

14. Finally, $TMP/sketh/xxx.ino.elf is processed to create $TMP/sketch/xxx.ino.hex which will be later uploaded to the Arduino board.

Simple!


HTH

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890
User avatar
Peter
QL Wafer Drive
Posts: 1253
Joined: Sat Jan 22, 2011 8:47 am

Re: Ino (Arduino) and CPP file differences - For Peter

Postby Peter » Fri Jan 29, 2021 11:57 am

Hi Norm,
many thanks, that's much more of an answer than I expected or deserved as a non-Arduino user who was just nosy. ;)
All the best
Peter


User avatar
NormanDunbar
Forum Moderator
Posts: 1493
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Ino (Arduino) and CPP file differences - For Peter

Postby NormanDunbar » Fri Jan 29, 2021 12:49 pm

I find it best to have too much information rather than not enough.

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890
martyn_hill
Super Gold Card
Posts: 662
Joined: Sat Oct 25, 2014 9:53 am

Re: Ino (Arduino) and CPP file differences - For Peter

Postby martyn_hill » Fri Jan 29, 2021 5:23 pm

Brilliant! Thank you , Norman!

I've been using the IDE for a couple of years now and had zero knowledge of what it was doing under the hood - very helpful!

So, if I'm writing code for the microcontroller in the IDE, does that mean I can now profess to being a 'C programmer' ? :-)


User avatar
NormanDunbar
Forum Moderator
Posts: 1493
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Ino (Arduino) and CPP file differences - For Peter

Postby NormanDunbar » Fri Jan 29, 2021 6:18 pm

Of course you can Martyn. Of course the Arduino Language, aka a library of functions, is just C/C++ code. Mind you, it does do quite a lot of work before it gets around to doing what it needs to. For example:

Code: Select all

pinMode(LED_BUILTIN, OUTPUT);


Does a whole pile ofwork before getting down to the nitty-gritty of:

Code: Select all

DDRB |= (1 << DDB5);


I've got the standard blink example down from 900+ bytes of flash plus 9 bytes of SRAM, to 28 bytes of flash -- just bypassing the Arduino stuff. Oh yes, and abbit of assembly! ;)

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890

Who is online

Users browsing this forum: No registered users and 3 guests