Library compilation.

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

Hello guys have a small problem.

I had made a project for my company where two atmega128 are talking to each other over the net.
Works perfectly and no problem.

Now today i begun a new project and copy the network project files to a new dir.
I made the main.c file like below

int main(void)
{
 return 0;
}

I compile it and the generated hex file was 8KB.

WOW how is that possible.
The only reason i can think of is that the compiler compiles all the other codes and libs i do not use and put them in my hex file.

So can any one explain it to me why a compiler compiles code and put it in a hex file but your not using all these functions and code.
And is there a way to tell the compiler not to do that.
Is waste of space and i only have 8K for bootloader
so it will never fit.

Many thanks for your help.

compiler is GCC
mcu atmega 128 futher i use winavr.

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

Pinkpanter wrote:
So can any one explain it to me why a compiler compiles code and put it in a hex file

Because that's what you told it to do?

Quote:
but your not using all these functions and code.

So why is it in your project?

If you're not using it, remove it from the project!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Because that's what you told it to do?

wrung question why it but unused function in hex file.

Quote:
So why is it in your project?

If you're not using it, remove it from the project!


But if i'm using only one function out of a big lib i cannot remove it.
So still my question why does the compiler compile all thestuff i do not use, my opion it is not correct.

So what you guys think about this.
Why does this happen

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

IIRC it is like this:

The linker will always link in all functions from a "compilation unit" even if only one of them is actually used. The term compilation unit is somewhat theoretical - you can replace it with "source C file". So, if you have libfuncs.c

int func1(void)
{
   // Some code here
}

void func2(void)
{
   // Some code here too
}

void func3(int i)
{
   // A here's some code too
}

and you compile this, and put it in a lib, and then have a myprog.c

#include "mylib.h"  // You can imagine what this contains...

int main(void)
{
   func2();
   return 0;
}

then code for all three functions will be included in the binary.

If you put the functions into separate source files (func1.c, func2.c and func3.c), compile them, and join the object files into a lib then, with the same myprog.c as above, only code for func2() (emanating from func2.c) will be included in the binary.

Put in another, more compact, way: The linkers "smartness" goes only to the level of compilation units (source files).

Quote:
Why does this happen

Bcause it was designed that way (if my memory of how things work is correct).
Quote:

my opion it is not correct

This might ony be a matter of not finding the right words, but... Correct or not correct is a binary, black/white, categorization. Although I cannot speak for the GCC implementors, I suspect that the level of "smartness" of the linker is a descision based on effort vs. tradeoff. It is also a wise technique not to put "too many" functions into the same source file, for other reasons than the one we are discussion. It is not uncommon to see coding standards ruling that a source file under normal circumstances should contain only one funtion (or one class if we are talking C++).

Practical suggestions:

1) If you only use one function from the lib, then do the dirty thing and copy the source code for that function into your project.

2) If you are to use several, but far from all, the functions in the lib then it might help if you divided the functions into several smaller source files. You could easily determine if my assumption is correct with a small experiment incorporating a few functions in separate files, built into a lib, but not all of them called. An analysis of the resulting hex file shoul tell you if my memory of how the librarian/linker works is correct.

3) Wait for someone with a better memory and/or knowledge than me, eg. Jörg, comes by and reves the full and true story...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Pinkpanter wrote:
But if i'm using only one function out of a big lib i cannot remove it.
So still my question why does the compiler compile all thes tuff

I think you're slightly mis-using the term "Library" here:

A Library is usually a collection of ready-compiled code from which the Linker can select only the required parts.
Such a Library has to be correctly organised to allow the the Linker to select only the required parts, and no others - perhaps yours is not correctly structured?

Or maybe you are talking about a source-code "library"?
Again, you need to correctly organise the source files in this "library" so that the required functions can be separated from the unused ones.

Compilers generally compile one source file to one object - so, if all your "library" functions are in one big source file, you will get one big object file!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Pinkpanter wrote:
I had made a project for my company where two atmega128 are talking to each other over the net ... Now today i begun a new project and copy the network project files to a new dir ... the generated hex file was 8KB
(my emphasis)

Well, if the "network files" provide full internet connectivity, I think 8K is pretty good! :shock:

And remember: the size of the hex file on your hard drive will always be significantly larger than the size of the binary image it represents - by a factor of over two!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Heeeeeeeeeeeee awneil made a good comment.
My mistake i'm not talking about a real library but about a

Quote:

source-code "library"

So now i understand why my code get so big.

Thanks for the reply awneil now i understand how it works.
Because i was also thinking about the printf lib.
Only when i use the printf function my code gets big.

Now i have all socket function and dhcp in a 6KB so with my extra bootloadercode it must fity in the 8KB the atmegas have for bootloader.

Quote:

And remember: the size of the hex file on your hard drive will always be significantly larger than the size of the binary image it represents - by a factor of over two!
Quote:

My mistake i meant the number you see when you compile your code.

bootloader.elf  :
section            size      addr
.data                 0   8388864
.text              6764         0
.bss                688   8388864

One small thing i'm not using a software stack but a hardware stack that is why it fits n 8KB.
Look at www.wiznet.co.kr for more info of the hardware stack.

So solution will be to make my big source code lib
in smaller source code files and than compile all the stuff. Than i will not have this problem.
Will do.

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

> Because i was also thinking about the printf lib.
> Only when i use the printf function my code gets big.

It's a real library (or rather: a module within a library).

Jörg Wunsch

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