Soumil Heble
Soumil Heble

Reputation: 25

How do I create a library where the linker only links functions used by my program?

I am compiling an application program for an 8-bit AVR micro-controller (AVR-GCC). To reduce the flash memory usage in a micro-controller it is essential that the linker only links functions which are used by the application code. I am writing my own library and I came across the following statement.

"Write one function per code module. This will compile to one function per object module" - Source: http://nongnu.org/avr-libc/user-manual/library.html

What does this mean? How do I create separate object files for each of my functions? Right now I only have one .h file and a .c file in my library.

Upvotes: 2

Views: 906

Answers (1)

unwind
unwind

Reputation: 399813

"One function per code module" means one function per C source file. That will give you one object file per function.

This seems awkward, the typical way to do this when using GCC is as described here:

  1. Compile with the -fdata-sections -ffunction-sections options, that tell GCC to put data and functions in separate sections. Sections are just a concept in the object files, basically a stand-alone region. A single object file can contain many sections.
  2. Then link with --gc-sections, telling the linker to garbage-collect unused sections. This will remove dead code.

Upvotes: 9

Related Questions