to see generated C code

Hi all,

I'm looking for a way to get C code from arduino, per my understanding when compiling and uploading the arduino sketch is first translated in C code (i.e. all required libraries, definitions and instructions are collected) which is then translated into assembler and uploaded to desk. Can I somehow check the generated C code? I found ways to see the assembler code, which is not really helpful...

per my understanding when compiling and uploading the arduino sketch is first translated in C code

Nope. The Arduino is programmed in C++. The C++ compiler, not surprisingly, knows how compile C++ code.

which is then translated into assembler and uploaded to desk.

Is that what you call your Arduino?

Can I somehow check the generated C code?

No. There isn't any.

Turn on verbose compilation logging. That will show you the directory where you compilation output is going. For example:

... /Applications/Arduino 1.0.1.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-gcc -Os -Wl,--gc-sections,--relax -mmcu=atmega2560 -o /var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp/sketch_apr29a.cpp.elf /var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp/sketch_apr29a.cpp.o /var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp/core.a -L/var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp -lm /Applications/Arduino 1.0.1.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 /var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp/sketch_apr29a.cpp.elf /var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp/sketch_apr29a.cpp.eep /Applications/Arduino 1.0.1.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-objcopy -O ihex -R .eeprom /var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp/sketch_apr29a.cpp.elf /var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp/sketch_apr29a.cpp.hex Binary sketch size: 2,524 bytes (of a 258,048 byte maximum)

Then go to the directory (/var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp) and look for the file with the .cpp extension and the same name as your sketch.

In my case the following three lines were added after the first comment and before the first line of code:

#include "Arduino.h"
void setup();
void loop();

I did turn the verbose output, but the .cpp file is generaly the same as the sketch, what I'm looking for is set of .cpp and .h files with all the used instructions collected. When I'm putting those together manually I keep finding references to more and more files, the compiler takes the code from those files, does it store the outcome before generating?

Not in a 'C' or C++ file. You could add compiler switched to output a map of the object code with assembly and code lines.

The libraries are not precompiled, they are source code (in C++ or C). That is why code is taken from everywhere. But the compiler removes all unused functions, so the resulting compiled code contains only what you need.

Erdin: Not in a 'C' or C++ file. You could add compiler switched to output a map of the object code with assembly and code lines.

The libraries are not precompiled, they are source code (in C++ or C). That is why code is taken from everywhere. But the compiler removes all unused functions, so the resulting compiled code contains only what you need.

Technically, it is the linker that removes the unused functions. The option '-ffunctions-section' is given to the compiler which puts each function into a separate ELF section (normally they are put into a single .text section), and the linker when the '--gc-sections' option is given, deletes sections that have no references.

In the build directory there are a pile of .d files:

$ ls *.d CDC.cpp.d Print.cpp.d WInterrupts.c.d new.cpp.d wiring_digital.c.d HID.cpp.d Stream.cpp.d WMath.cpp.d sketch_apr29a.cpp.d wiring_pulse.c.d HardwareSerial.cpp.d Tone.cpp.d WString.cpp.d wiring.c.d wiring_shift.c.d IPAddress.cpp.d USBCore.cpp.d main.cpp.d wiring_analog.c.d

Each of those files seems to contain the list of source files (.cpp and .h) that when into the building os an object (.o) file. For example:

\ /var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp/sketch_apr29a.cpp.o: \ /var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/build5065097978620177293.tmp/sketch_apr29a.cpp \ /Applications/Arduino\ 1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Arduino.h \ /Applications/Arduino\ 1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/binary.h \ /Applications/Arduino\ 1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WCharacter.h \ /Applications/Arduino\ 1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WString.h \ /Applications/Arduino\ 1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/HardwareSerial.h \ /Applications/Arduino\ 1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Stream.h \ /Applications/Arduino\ 1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h \ /Applications/Arduino\ 1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Printable.h \ /Applications/Arduino\ 1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/new.h \ /Applications/Arduino\ 1.0.1.app/Contents/Resources/Java/hardware/arduino/variants/mega/pins_arduino.h

You could probably parse those files to find out all the source files used in your project.

You can also change where the build directory is. Open the ArduinoIDE and then select menu:File/Preferences (see attached image for reference). At the bottom of this window is the path for the preferences file that you can edit. On my system (Win7-64bit with Arduino v1.5.2) that path is actually clickable and opens the folder where the preferences.txt file is. Note, when you close the ArduinoIDE the file will be overwritten with the current session settings. You can edit the file w/o closing the IDE, but once the IDE closes any changes you made will be lost…

You will want to add (or modify) a line that starts with “build.path=”. What I did was create a folder called “Build_Box” in my regular sketch folder. I then made that line:

build.path=C:\Users\cdelliott\My Development\Arduino\Build_Box

Now whenever I compile, all the stuff happens there. It just makes it easier for me to grab the hex files so I can transfer them to my tablet and use ArduinoCommander to upload the binary w/o having to spend time re-compiling. (I haven’t played with ArduinoDroid enough to be sure of it’s compiling performance, but that is for another thread somewhere else in the forum hierarchy…)

ScreenClip - Arduino Preferences.png

y100921: Can I somehow check the generated C code?

Yes. If you use File / Preferences to open the preferences dialog and tick the 'show verbose output during compilation' option, when you 'verify' sketches subsequently the IDE will display various status and information messages that include the name of the directory that the C++ files are being compiled in. You can open that directory yourself and view the various files the IDE puts there - they would include all the standard Arduino runtime libraries and additional libraries used by the sketch, plus the C++ file that the IDE generates from your sketch file and any other .cpp or .h files you added to your project.