you should turn on a more verbose output, so you can see all command-line
tool invocations. Enable verbose output for both compilation and upload in
the Preferences menu, as described in Changing Preferences, on page 26.
Then load your blinking LED sketch and compile it. (We did this back at the
start of our journey in Changing Preferences, on page 26).
The command invocations look weird at first because of the names of the
many temporary files that are created. You should still be able to identify all
compile and link steps necessary to build even a simple sketch like our
blinking LED example. That’s the most important thing the Arduino team
did: they hid all these nasty details well behind the IDE, so even people with
no software development experience can program the Arduino. For program-
mers, it’s a good idea to work in verbose mode, because the best way to learn
about all the AVR tools is to see them in action.
Upload the program to the Arduino now to see
avrdude
in action. This tool is
responsible for loading code into the Arduino and can be used for programming
many other devices, too. Interestingly, the AVR tools even make it possible
to use the Arduino IDE for non-Arduino projects.
There’s another difference between Arduino programming and regular C++
code. When programming for the Arduino, you don’t define
main
yourself,
because it is already defined in the libraries provided by the Arduino develop-
ers. As you might have guessed, it calls
setup
first and then runs the
loop
function in a loop. Since Arduino 1.0, it also calls
serialEvent
at the end of the
loop function.
Further restrictions when programming C++ on AVR microcontrollers include
the following:
1
• You cannot use the Standard Template Library (STL) because it’s way too
big for the small AVR microcontrollers.
• Exception handling isn’t supported. That’s why you see the
-fno-exceptions
switch often when the
avr-gcc
compiler is invoked.
• Dynamic memory management using
new
and
delete
isn’t supported.
In addition to all that, you should keep an eye on performance. C++ automat-
ically creates a lot of functions (copy constructors, assignment operators, and
so on) in the background that are rarely needed on the Arduino. Even with
these restrictions, the Arduino supports a powerful subset of the C++ program-
ming language. So there’s no excuse for sloppy coding!
1.
http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_cplusplus
Appendix 2. Advanced Arduino Programming • 250
report erratum • discuss
www.it-ebooks.info