Shop OBEX P1 Docs P2 Docs Learn Events
printf went and ate all my bytes ;) — Parallax Forums

printf went and ate all my bytes ;)

photomankcphotomankc Posts: 943
edited 2012-08-01 08:51 in Propeller 1
I was wondering if there are some pointers on what to look at in your project when printf() starts adding in ~20KB to the code? I have other projects that include "stdio.h" and use printf with the "Simple printf" option checked and the code size stays much smaller. My current project however if I comment out any printf statements compiles to about 5.2KB, when I put the statements back in we jump back up to about 25KB using either LMM or XMMC.

Don't see an obvious difference in how I included it or used it.
/*
    Small test program for I2C bus driver.
 */

#include <stdio.h>
#include <unistd.h>
#include <propeller.h>
#include "I2CBus.h"

/**
 * Main program function.
 */
int main(void)
{
    int addr = 0;
    int result = 0;
    I2C i2cBus(28,29,400000);
    
    sleep(2);
    printf("<<<< I2C Driver Test Program >>>>\n");
    printf("  Bus operating on COG: %d\n\n", i2cBus.getCog());
    sleep(2);
    printf("  Bus Scan:\n");
    for (; addr < 128; addr++)
    {
        i2cBus.begin(addr);
        result = i2cBus.end();
        printf("  I2C Address - %x", addr);
        
        if (result == 0)
            printf(" <--- Device Found \n");
        else
            printf("\n");    
            
        usleep(75000);
    }    

    return 0;
}

Comments

  • David BetzDavid Betz Posts: 14,516
    edited 2012-08-01 08:06
    This might be caused by non-trivial printf statements vs. trivial ones that just print a constant string. If you do something like this:
    printf("Hello, world\n");
    
    the compiler will replace the call to printf with a call to puts which is much smaller. However, if you use % parameters in your printf call the full printf will be included.
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-08-01 08:15
    One of your printf's contains a call to i2cBus.getCog(). It may be that referencing this routine is pulling in 20K of code.
  • photomankcphotomankc Posts: 943
    edited 2012-08-01 08:19
    Ok. That makes some sense but the hello.c sample does this:
    #include <stdio.h>
    #include <propeller.h>
    
    int main(void)
    {
        int n = 1;
        while(1) {
            waitcnt(CLKFREQ/10+CNT);
            printf("Hello World %d\n", n);
            n++;
        }
        return 0;
    }
    

    As shown it compiles to 7.2KB
    Comment out printf and it compiles to 2.5KB.

    I think you are right, something somewhere is dragging in the full printf but I'm just not sure what since that top .cpp file in my example is the only one doing any printf-ing. I tried printing all values in decimal too but it seemed to make no difference.


    Oh... Dave, I just noticed it must be your I2C code I'm tearing up from the PropBOE examples! That was a nice a springboard to start from!
  • jazzedjazzed Posts: 11,803
    edited 2012-08-01 08:25
    photomankc wrote: »
    I was wondering if there are some pointers on what to look at in your project when printf() starts adding in ~20KB to the code?

    For LMM use Ted's libtiny.a found here: http://forums.parallax.com/attachment.php?attachmentid=91520&d=1333953456
    An XMM library has not been created for it yet. You should not expect libtiny.a to work with XMM.

    To add the tiny library in SimpleIDE for LMM you have 3 options:

    1) Right click a file name and "Add Library File"
    2) Put libtiny.a in your project, then add -ltiny to SimpleIDE Other Library Options.
    3) Put libtiny.a in a library folder and "Add Library Path" on project manager, then add -ltiny to SimpleIDE Other Library Options.
  • photomankcphotomankc Posts: 943
    edited 2012-08-01 08:28
    Dave Hein wrote: »
    One of your printf's contains a call to i2cBus.getCog(). It may be that referencing this routine is pulling in 20K of code.

    Comment that out and it's still the same. But as Dave mentions if I do only static text AND leave "Simple printf" UNCHECKED the code drops to 9.8KB If I do static text and have "Simple printf" checked it goes to 25KB. If I do ANY %d, %X stuff then checked or unchecked balloons to 25-35KB.

    Odd.
  • David BetzDavid Betz Posts: 14,516
    edited 2012-08-01 08:51
    photomankc wrote: »
    Comment that out and it's still the same. But as Dave mentions if I do only static text AND leave "Simple printf" UNCHECKED the code drops to 9.8KB If I do static text and have "Simple printf" checked it goes to 25KB. If I do ANY %d, %X stuff then checked or unchecked balloons to 25-35KB.

    Odd.
    I think this can be explained by the fact that checking the "Simple printf" box causes all occurances of "printf" to be replaced by "simple_printf". The compiler doesn't know how to turn that call into puts so it always calls the full simple_printf function.
Sign In or Register to comment.