Electronic – Strategies for managing code size – PIC18 XC8


I'm new to embedded C development and I'm looking for some general principals/guidelines for managing the size of the generated code.

In my specific example I'm using XC8 on a PIC18F4550. My code so far uses about 13% of the space. Once I use "time.h" and 1/2 functions from it, the usage jumps to 45%! I realise that the free XC8 compiler does not optimise but I'm still very surprised to see just how much space is used.

What I'm looking for a tips / staring points along the lines of

  • Alternatives to the standard libraries that implement on small targeted subsets of the functionality. E.g. a date/time lib. i.e. are there repositions I can get useful routines from?
  • How to see what is using all the space. (Listing/map files – and tips for analysing them)
  • Any other tips / resources.

I found a nice implementation without using stdlib here: https://codereview.stackexchange.com/questions/38275/convert-between-date-time-and-time-stamp-without-using-std-library-routines

The general question still stands though. Are there guidelines, good repository sources etc

Best Answer

Here are some things I have done when using Microchips C18 compiler. Maybe the concepts will apply to your compiler: - declare each string one time. Put in a separate .c file and use a .h file to reference them. In other words don't duplicate display messages multiple times. - declare strings so that they go in rom. This wont help you with ROM (code space) but it will save memory. - write tight code. If several lines of code are duplicated, put them in a function. Copy / paste is not necessary an embedded programmers best friend. - like you already thought of, implement your own functions instead of including a library. I once saved a ton of space by creating my own itoa() function. Try not to use printf.