Home made library produces too large code

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I searched forum but found nothing about this problem.
When I compiled these two source files, I got 700 bytes code.

/* File:     main.c    */
/* Procesor: Atmega8   */
/* Crystal:  10 MHz    */
/* Compiler: Avrstudio ver. 4.13 build 528 + Winavr-20070525  */

unsigned int D1,D2;
float pressure, temperature;

float calc_float(unsigned int arg1);

int main(void)
{
        for(;;)                     
   	{
        pressure    = calc_float(D1);          
        temperature = calc_float(D2);          
        }
}//main

-------------------------------------------------------
/* File:     calc_float.c  */

float calc_float(unsigned int arg1){
float fd1;

    fd1 = (float)arg1;
    return 100000.0 / fd1;
}

Then I created library from file "calc_float.o":
avr-ar rcs libmyfloat.a calc_float.o

But then when I compiled with library libmyfloat.a,
the code was 1500 bytes. It increased twice!!

I have experimented with another moduls in my library and
I found that only functions with float math enlarged the code.

In Project-Options-Libraries I added libc.a and libm.a.

What am I doing wrong?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are using floating point arithmetic !

As the AVR does not have a floating point unit it uses floating point libraries. Tey are a certain size and they does blow up your program.

Try the exactly same program, but using 'int' instead of float and you'll see it shrink immediately.

Markus

Markus

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Markus, the point is, that when he links it directly (as a .o file, the result is 700 bytes. When he converts it to a .a file, and then links it, the result is twice that. The only thing I can think of is that libm is somehow getting included twice here.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Marcus, I will try to explain the thing better.
I use only one function in main, calc_float().
When I write that function direct to main file, the code is 700 byte.
But when I use the very same function from library, the code is 1400 byte.
That is, what makes me sad.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

glitch, you have hit the nail on head.
I have removed libm.a from Project-Options and success!!
Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Today I have played again with the library and I discovered a surprise thing.
In program above I link with libraries libc.a and libmyfloat.a. And I found that code size depends on the order in whitch the libraries are placed in Studio-Project Options-Libraries.

This produces 700 bytes:
libmyfloat.a
libc.a

When I change the order:
libc.a
libmyfloat.a

the code grows to 1400 bytes.
Strange thing for me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Remove libc.a, it doesn't belong there. The compiler will automatically
include it at the appropriate place.

Yes, library processing is position dependent.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks fo replay, Joerg. But when I remove libc.a, as you suggest, and allow only my library, the code grows again considerably.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Then analyze it. Have a look at the symbol table, and see what function
causes the bloat (avr-nm --size-sort -S is helpful for this). Don't
forget to *always* link against libm.a (last in sequence) for any
floating-point application. In fact, it would be recommendable to always
include that library into any of your projects.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So finally I have built with
libmyfloat.a
libm.a
and all is in order.
Thank you again.