Go Down

Topic: make your Arduino 0007 sketch smaller! (Read 3305 times) previous topic - next topic


Announcing a new version of the library patch that makes your Arduino sketches smaller.  If you are running out of room in your sketch, and you want to squeeze in more of your own code, this may be just the thing you need:


Using this patch, I was able to make one of my own sketches go from 6986 bytes (using official Arduino 0007 distribution) to 4806 bytes!

Please note that this patch is not compatible with the command-line build process; please use this only if you are using the Arduino IDE to build your sketches.  Also, it has only been tested so far on my Windows machine.  If you follow the instructions carefully, you can easily uninstall this patch if it breaks your stuff.  But I need brave souls to help me test this for Linux and Mac.

Please post any bug reports, questions, and suggestions to this forum topic... thank you!

- Don Cross

I don't know if it was even considered in the design of the patch, but I'm running Arduino with an ATMega168, and I get the error:

Code: [Select]
/Applications/arduino-0007/tools/avr/bin/../lib/gcc/avr/4.0.3/../../../../avr/lib/avr5/crtm168.o: In function `__bad_interrupt':

../../../../crt1/gcrt1.S:123: undefined reference to `main'

Couldn't determine program size: tools/avr/bin/avr-size: '/tmp/build38608.tmp/led_blink.hex': No such file

when I try to compile. Is it possible to use this patch for the ATMega168, and are there any other ways to eliminate 1 KB in Arduino0007?
Thanks in advance,



Feb 16, 2007, 03:49 pm Last Edit: Feb 16, 2007, 03:55 pm by CosineKitty Reason: 1
Hmmm... I just tried compiling for Atmega168 on my own machine and everything works fine.  Can you tell me...
  • What operating system you are using?
  • Are you using the Arduino GUI or the makefile?
  • Is it possible to post a recursive directory listing of your Arduino install root?

Thanks for the response,

I'm using Mac OS 10.4.8, and I am using the Arduino GUI for everything.
Here is the directory listing. To install it, I moved the processing directory to /lib/processing and I replaced the /lib/targets/arduino folder with the provided one.

Code: [Select]
Freds-Computer:/Applications/arduino-0007/lib fred$ ls -R
about.jpg               processing              tab-unsel-left.gif
buttons.gif             resize.gif              tab-unsel-menu.gif
icon.gif                tab-sel-left.gif        tab-unsel-mid.gif
keywords.txt            tab-sel-menu.gif        tab-unsel-right.gif
loading.gif             tab-sel-mid.gif         targets
preferences.txt         tab-sel-right.gif


Compiler.class  Compiler.java   preproc

PdePreprocessor.class   PdePreprocessor.java

arduino         atmega8         blank           libraries       wiring

HardwareSerial.cpp      WRandom.cpp             wiring_analog.c
HardwareSerial.h        binary.h                wiring_digital.c
Makefile                main.cxx                wiring_private.h
WConstants.h            pins_arduino.c          wiring_pulse.c
WInterrupts.c           wiring.c                wiring_serial.c
WProgram.h              wiring.h                wiring_shift.c



Matrix          SHT_Library     SoftwareSerial  Sprite          Wire

Matrix.cpp      Matrix.h        Matrix.o        examples        keywords.txt

hello_matrix            sprite_animation



SHT_Library.cpp SHT_Library.h   SHT_Library.o

SoftwareSerial.cpp      SoftwareSerial.o        keywords.txt
SoftwareSerial.h        examples


Sprite.cpp      Sprite.h        Sprite.o        binary.h        keywords.txt

Wire.cpp        Wire.o          keywords.txt
Wire.h          examples        utility

SFRRanger_reader        master_reader           slave_receiver
digital_potentiometer   master_writer           slave_sender

SFRRanger_reader.pde    applet

HardwareSerial.cpp.o    SFRRanger_reader.hex    WRandom.cpp.o
SFRRanger_reader.cpp    SFRRanger_reader.pde    pins_arduino.c.o
SFRRanger_reader.cpp.o  SFRRanger_reader.rom    wiring.c.o
SFRRanger_reader.elf    WInterrupts.c.o






twi.c   twi.h   twi.o

Binary.h                QSlide.o                WProgram.h
Encoder.h               Servo.h                 WRandom.o
Encoder.o               Servo.o                 WTimer.o
HardwareSerial.h        Sprite.h                buffer.h
HardwareSerial.o        Sprite.o                buffer.o
LiquidCrystal.h         TwoWire.h               twi.h
LiquidCrystal.o         TwoWire.o               twi.o
Matrix.h                WApplet.o               uart.h
Matrix.o                WConstants.h            uart.o
QSlide.h                WInterrupts.o



Hello again Fred,

One thing the library patch relies on is that installing the files Compiler.class and PdePreprocessor.class should override the classes inside the supplied jar file.  Maybe for some reason this is not the case in the Macintosh environment?  Here is one thing to check:  Go into the directory where the failed compile takes place (e.g. /tmp/build38608.tmp/, whatever it says in the error message), and look at the file led_blink.cpp.  At the very bottom of that file, you should see the following:
Code: [Select]

   main.cxx  -  Startup code for Arduino, slurped in at last moment by Arduino preprocessor.
   Split out from wiring.c by Don Cross (CosineKitty on the Arduino forums).
   We give this the funny extension .cxx so that source code editors can recognize it as
   C++ code (for syntax highlighting, etc.), but not .c or .cpp, so that it is excluded
   from the list of files to be compiled into a library.

int main(void)
   init();         // initialize hardware and data structures for runtime library

   setup();        // call user-defined setup routine
   for (;;)
       loop();     // keep calling user-defined loop routine forever
   return 0;       // we never get here, but make the compiler happy about main returning int

The fact that the linker says that it could not find main() makes me suspect that this code has not been appended properly to your sketch.  This is the job of PdePreprocessor.class.  I suspect that you are still running the original Arduino 0007 preprocessor, not the patched replacement.

Is there anyone out there that understands better how the Arduino Java code works on Macintosh?



The fact that the linker says that it could not find main() makes me suspect that this code has not been appended properly to your sketch.  This is the job of PdePreprocessor.class.  I suspect that you are still running the original Arduino 0007 preprocessor, not the patched replacement.

Is there anyone out there that understands better how the Arduino Java code works on Macintosh?

I'm getting the same error... This seems like a worthwhile patch... if anyone has any insight on this... It would be appreciated.
there are 10 types of people in this world, those who can read binary and those who can't.


You probably need to open up the pde.jar in the Arduino.app file (right click on it and select show package contents), then replace the appropriate .class files.  I don't think the lib directory is in the classpath on the Mac.


OK everyone, I am trying out Mellis's idea:  I updated the pde.jar file with the 3 classes I modified (Compiler, PdePreprocessor, Sizer).  Please help me test this on Macintosh.  Here is how:

(Note: these instructions assume you already have the library patch installed!)

1. Exit Arduino GUI if running.
2. Go into your Arduino 0007 install path, then into its "lib" directory.
3. Rename pde.jar to pde.jar.original (for safe keeping).
4. Download the replacement pde.jar (773 K) from:


5. Go back into Arduino GUI and try compiling again.  Please post results of experiment here.

If this works, I will update the library patch for everyone to use.

Thanks for helping this poor Mac-less person!

- Don


Can't find pde.jar in my installation (Arduino 7 for Mac OS X 10.4.8  PPC, downloaded about 5 minutes ago):

Code: [Select]
imac:/Applications/arduino-0007 jdw$ find . -name *.jar -print
./Arduino 07.app/Contents/Resources/Java/antlr.jar
./Arduino 07.app/Contents/Resources/Java/Arduino.jar
./Arduino 07.app/Contents/Resources/Java/mrj.jar
./Arduino 07.app/Contents/Resources/Java/oro.jar
./Arduino 07.app/Contents/Resources/Java/registry.jar
./Arduino 07.app/Contents/Resources/Java/RXTXcomm.jar
imac:/Applications/arduino-0007 jdw$

At a glance, it looks like your pde.jar has most of the same components as Arduino.jar and then some, but they are not identical.

Code: [Select]
imac:~ jdw$ jar tf /Applications/arduino-0007/Arduino\ 07.app/Contents/Resources/Java/Arduino.jar  | sort >a.j
imac:~ jdw$ jar tf pde.jar | sort >p.j
imac:~ jdw$ diff a.j p.j
> arduino/
> arduino/app/
> arduino/app/preproc/
> arduino/app/syntax/
> arduino/app/tools/
> processing/app/ArduinoMessageSiphon.class
> processing/app/Command.class
imac:~ jdw$

Looked pretty close, so I tried it anyway, renaming your pde.jar to Arduino.jar.  When I compile, I get:

Code: [Select]
2: error: #error Arduino preprocessor could not open file lib/targets/arduino/main.cxx

Thanks for helping this poor Mac-less person!

I always try to help the less fortunate.  :)  Especially when they're trying to do me a favor.



Feb 19, 2007, 04:27 am Last Edit: Feb 19, 2007, 04:29 am by greyskale Reason: 1
same here... no pde.jar to be found  :(

the contents of my app are:

there are 10 types of people in this world, those who can read binary and those who can't.


Whoops.  I meant Arduino.jar not pde.jar.  I don't know why it didn't work when you tried renaming it.  In any case, this patch will be incorporated into the next release of Arduino.


Just for grins I installed pde.jar on a Windows machine at work.  Same results - compile dies with the same message.



Please note that pde.jar on Windows is not sufficient (or even necessary).  There are several components to this library patch, including modifications to the runtime source code in addition to the Java code changes.  On Windows, please ignore this jar stuff for the time being: just follow the instructions at:


The Macintosh problem is going to require some more thought on my part.  Specifically (Mellis are you reading?) I need to figure a way to access the fully qualified path to the Arduino installation from inside the PdePreprocessor class.  I was hoping that what worked on Windows (using relative path name) would also work on Macintosh, but apparently it doesn't.


Dunno, maybe you can incorporate the changes I made when I integrated it into the Arduino svn?  Or just wait until 0008 comes out?


Feb 19, 2007, 04:44 pm Last Edit: Feb 19, 2007, 11:30 pm by CosineKitty Reason: 1
Thanks for the quick reply Mellis!  I just looked at your version of the changes and I just now realized you do seem to have solved the Macintosh problem: your code passes the target directory to PdePreprocessor.writeFooter().  Too bad I didn't notice that before now!   ::)

Anyway, I'm working on integrating your Java code back into my library patch now.  I will post again here as soon as I have something ready for the Macintosh people to try again.

- Don

NOTE:  This thread continues on another page:
I have now posted a new jar file for Macintosh users!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131