Compile code on the arduino

Is it somehow possible to compile new code on a Arduino Mega? Meaning that there are not cable connected to it, and you're communicating with it via e.g. an ethernet board, is it possible to have a compiler on the Arduino?

Thanks in advance.


Rensen: Is it somehow possible to compile new code on a Arduino Mega? Meaning that there are not cable connected to it, and you're communicating with it via e.g. an ethernet board, is it possible to have a compiler on the Arduino?

Depends on what you mean by "compile code". If you mean compile Arduino sketches to native AVR, then no, mainly because the Mega has too little memory and CPU for that.

But if you had a sufficiently small language you could compile programs on the Mega, but it would have to be quite a primitive language You would also need a bootloader to write the new code into flash.

A more practical way would be to use an interpreted language, this could be more flexible but slower to execute.

If you want to compile and download sketches remotely, you might be able to do this with a Raspberry Pi or similar. Although in that case you could just skip the Arduino and run scripts on the Pi/Beaglebone etc.

ETA: Some info from the playground

A more practical way would be to use an interpreted language,

One such is called bitlash

pepe: Remember that thirty years ago, a 8-bit CPU could already run a computer with C compiler, text editor, text-mode HMI and communication system (at the same time), despite less computing power and less memory (RAM/ROM) than with the Arduino Mega 2560 today.

Wow, you are right! I had forgotten about Small-C, that was around 1980. There were C compilers for Apple II and BBC Micro. 8KB is possibly a little on the small side, ISTR those computers had up to 64KB.

One of my first programming jobs was on an IBM PC, 512KB and massive 5MB hard disk! That was around 1986 though :)

Of course it helped that they were von Neumann architecture so complied code could be run in RAM, unlike AVR with separate Program and Data buses (Harvard).

So, I think a tiny-C for Arduino is definitely on the cards :) Heck, someone was running Unix on a PIC, so a C compiler should be easy!

The architecture of those chips was fundalementally diffrent to that in the AVR. Look up what a Harvard architecture means.

Also, not only did those systems have lots more RAM than the AVR but they also used files on a disk (floppy) to do things just like our more modern systems. I'm thinking you guys that are thinking this is possible on an AVR don't understand how programs and embedded images are built. Here are the basic steps to compiling and linking an image: - cpp processes the .c or .cpp original source file to creates the pre-processed file it will need access to all the files are included using #include.

  • the compiler processes/compiles the cpp files and creates the assembler code in a file.
  • The assembler runs and builds the .o
  • This repeats for all the files being built.
  • The linker processes all the .o files and links against any pre-built libraries (like the C startup & runt time libraries) to create a linked image.

Now toss in Arduino and the way it works and you have even more "mess" to deal with. There is a LOT of goofy stuff that the IDE does to turn a .ino file into a .cpp file. This requires access to all the library directories (both the IDE supplied one and the user sketchbook one).

Then because the Arduino system doesn't really use libraries, (Other than for the "core" code, Arduino doesn't use real libraries, it builds all the files in its "libraries" and links against the individual .o files for each "library" used) the IDE must compile all the needed files for the "libraries" it happened to notice while doing that first pre-processing step.

So even if you had more RAM, there would not be enough FLASH in the part to hold all the source code to all the "libraries" which have to be scanned and built. Bring up the IDE and build a sketch then go look down in the tmp directory at all the files created. In order to build an image you would need to have room for all those files. The build process could be streamlined and made smarter to use real libraries which would eliminate having to compile "library" code modules but even if you did that there still wouldn't be enough room to hold the .a library files much less all the include files needed to compile the sketch.

Even if you used an SD card for all the files, there still isn't enough ram to able to support all the internal data needed to run something like a C pre-processor or a compiler.

It just isn't possible.

As far as the PIC running unix goes, if I remember correctly the PIC was not actually running linux but emulating an ARM system which ran linux. - This is not the same thing. Since it was emulating, it could also create a h/w interface to use more ram.

--- bill

Fundamentally with a Harvard architecture the program space and data space are separated unlike the normal computer system where the two spaces are the same thing. That is what you are not understanding.

This means that the only thing that can run code is the flash memory space. This can not be expanded in any way. SD cards are paged data memory and extra ram can be paged or put into the data space ( on a Mega only ).
Therefore there is no possibility of having enough room to run a compiler. Add to that the fact that the only way to write to the flash memory is from within the boot loader page and the answer is no way sunshine.

Well if you feel like that then you answer the original question put by Rensen then.

You tell him how to do it, you provide the code; stop wafting airy fairy arguments about and show him how to do it. Put your money where your mouth is. Until you do then my NO answer stands. Otherwise you are wasting everyone's time.

He just wants the Arduino to compile programs for Arduinos.

If that is indeed what he wants then it is surely a pointless exercise is it not?

You say NO ? I say YES. If you don't agree, just try to prove I am wrong !

How can I prove something can not be done? You can not prove a negative.

Your lack of understanding of the fundamentals of scientific logic appears to be as great as your lack of understanding of computer architecture.

Unless you have anything remotely sensible to say I will assume this thread to be closed.

OK, I'm going to stick my neck out here: Yes, it is feasible to compile programs on an AVR (If you can emulate an ARM and boot Linux on an AVR, then just about anything is possible). However, it isn't sensible in any reasonable timescale or budget.

Thread closed.