3

Library.h

void Foo1(void);        // Unused
int  Foo2(int, int);    // Used
char Foo3(char);        // Unused

main.c

// ...
#include "Library.h"
// ...
void main(void)
{
    int ret;
    // ...
    ret = Foo2(3, 7);
    // ...
}

I have a library file which has a lot of function definitions in it. Will the generated machine code size increase because of the unused library functions? Or will the compiler optimize the code by ignoring those unused functions?

IDE: MPLAB 8.43
Compiler: MCC18 3.34
PIC: 18F2550

hkBattousai
  • 10,583
  • 18
  • 76
  • 124
  • 3
    Most linkers only can omit an entire object file, not individual functions. Simplest way to find out is just try it. – Hans Passant Feb 19 '12 at 17:25
  • Unused functions are _not_ added to the executable unless the linker is trash. That being said, some linkers are trash. – Lundin Sep 05 '17 at 09:55

2 Answers2

2
  1. Check. Use a disassembler or some other tool to look at your output binary and find out.
  2. Find out if your linker has an option to do dead-stripping.
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

For C18 member in lib is .o - so if your link requires one function from a member the whole (all functions from) .o is included in final bin/hex code.