Seeing the C code

Is there a way to see the actual "C" code that Arduino produces during the upload process? That would be helpful to us newbies.

Thanks.

The C code is what you write. The sketch language is C++ or C.

The compiler reads the C code and makes object files.

The object files get linked together into an executable file (called an elf file).

The bytes in the elf are almost ready for uploading. There's extra stuff that you don't need, so they strip it down and put it in a convenient upload form called hex. It's the hex file you upload.

Given an elf file, you can run avr-objdump to get a disassembly listing that mixes your original C/C++ code with a listing of the assembler/machine instructions that implement each statement.

The Arduino IDE, by default, occasionally deletes elf files to save space and clutter. You can grab them just after the sketch is verified (built), by visiting the sketch's subdirectory.

If you want to see the assembled source file that is produced by the IDE's preprocessors, it'll be in your sketch folder inside a directory named "applet". The file will be your project name with a ".cpp" extension.

This is the file that is ultimately compiled and turned into a .hex file that is uploaded to the chip.

Um, two apparently quite different replies? I must be missing something?

I thought Arduino was a version of the "Processing" language, which was not C. Please be very clear, I am handicapped by being exposed only peripherally to many languages and am consequently confused by what is what.

In the long run, I would like to use the Duemilanove to learn to write C code. I am hopeful that the code I write that gets uploaded to the board can be observed in its "pure" C state so I can see how to writhe the "real" thing. Again, I may be completely missing something.

Thanks again for the replies.

Arduino code is a combination of C and C++.

So you can write straight C code if you want, include C++ code or constructs, even include AVR assembly.

So if you want a pure C environment for learning, then Arduino is not that.

Will I be learning "bad" coding technique, style, or habits by using Arduino? Will it be hard to later have to use pure C?

You'll definitely be seeing and learning things that won't compile under "pure" C.

k. Thanks for the input.

Will I be learning "bad" coding technique, style, or habits by using Arduino? Will it be hard to later have to use pure C?

Yes, the Arduino IDE does some preprocessing that you need to work around when using "proper" C. Like others have said, the IDE concatenates all the .pde-files in your project into one big cpp-file which is then compiled by the c(++)-compiler. It also creates prototypes for all functions, but inserts them before any typedefs so you can't use your own datatypes in function headers without putting the typedefs in a separate .h-file.

IMHO the strength of the Arduino environment is the libraries and the ultra-cheap hardware. The downside is the preprocessing of code - I would have preferred to stick to "proper" C/C++.

Well as a relative new comer to the Arduino and heavy on hardware experience but weak on programming I found the Arduion C/C++ Lite to be great for me. It's lets me progress at a slow pace without having to master too many things at first just to get some production out of it.

I'm sure a path to full blown C/C++ IDE would be liked by some, but frankly I haven't seen anything the IDE platform can't make the AVR chip do including interrupt support, assembly insertions, and access to all the AVR support peripherals. Bottom line I think the IDE matches the hardware capabilities just right. If the board required a RTOS or something then maybe a more conventional IDE might be useful, but then that's not really something one should be using a 8 bit chip for when the 32 bit chips are getting so cheap these days. Just noise from a Arduion fan ;)

Lefty

The only thing that differs between "Arduino-C" and "proper" C is that on the Arduino, you don't have to declare prototypes for functions. I see the point in doing this, but the way it's implemented is not very good. You no longer have the ability to create prototypes and typedefs in the desidered order without having to resort to separate .h-files. From my point of view this makes things harder, not easier.

Also, the GUI doesn't make it clear that the contents of tabbed pages in a project really is parts of the same file. Unless you look behind the curtains you wouldn't know about this, and for a C-programmer the difference is significant.

That said, I really like the Arduino and the IDE. The IDE is very basic, but that suits the Arduino really well. No need for a overly complex IDE when you're dealing with projects with hundreds of lines and not millions :-)

I would have preferred to stick to "proper" C/C++.

So can't you have your sketch read:

void setup (void) { my_c_setup(); }
void loop (void) { my_c_loop(); }

and just more your "pure" code to a file in the sketch directory with a .c or .cpp extension? My impression was that the IDE only does pre-processing on the files with .pde extensions...

My general impression is that you won't be learning any bad habits other than those inherent to C/C++ itself. You might be missing out on a good habit (prototypes for forward references), but that's pretty minor.

and just more your "pure" code to a file in the sketch directory with a .c or .cpp extension? My impression was that the IDE only does pre-processing on the files with .pde extensions...

And how do you tell the IDE to compile and link those c/cpp-files? If I put myloop.h and myloop.c in the sketch folder, and include myloop.h in the sketch, the IDE will compile myloop.c but linking will fail.

Anyway, I don't mean that Arduino sketches are not "pure", I just want to point out what I think is some serious short-comings of the IDE preprocessing.

Given an elf file, you can run avr-objdump to get a disassembly listing that mixes your original C/C++ code with a listing of the assembler/machine instructions that implement each statement.

How to dump to file instead of screen?

How to dump to file instead of screen?

You can redirect the output to a file. I posted a batch script that show one way to do this: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207951658/2#2

Gotcha!