1

What code goes into the final executable when using a library?


As an example, we have two files:
/*main.c*/
int main (int argc, char* argv[]){
    fc(1); /*This function is defined in fc.c*/
}

Another file:

/*fc.c*/
int fc(int x){
    return fe(x);
}
int fe(int y){
    return y + 1;
}

We compile fc.c:

gcc -c fc.c

We then get fc.o.

Now lets build a library named test:

ar rcs libtest.a fc.o

We now have libtest.a.

Now we compile main.c

gcc -c main.c

And we obtain main.o

Let's link our main.o to our libtest.a

gcc -L. main.o -ltest

We get the desired a.out

Checking it's symbols:

nm a.out

In between all the symbols, we find:

080483cc T fc
080483df T fe

Seems good. BUT!

If our main.c changes for this?

/*main.c*/
int main (int argc, char* argv[]){
    fe(1); /*This function is defined in fc.c*/
}

After compiling main.c and linking the new main.o to our library, I will still find a symbol for fc. But I don't need that code.

Questions

-Shouldn't the library "give me" only the code I need in main.c?
-Do the functions need to be in separate modules before being added to the library?
-What if I had 300 functions? Would I need to make 300 modules?

n233g16
  • 45
  • 1
  • 5
  • Why would you need 300 modules? You can have 300 functions in a single one. – Iharob Al Asimi Jan 06 '16 at 16:55
  • 1
    @ iharob because if they are all in separate modules, linking a program with the library will load only those functions referenced by the program (and recursively). If they are all in one module, linking will load in all 300 of the functions, even if only one function in the library is called by the program.. – FredK Jan 06 '16 at 17:07
  • Possible duplicate of http://stackoverflow.com/questions/6687630/how-to-remove-unused-c-c-symbols-with-gcc-and-ld/6687656#6687656 – kfx Jan 06 '16 at 17:47

2 Answers2

2

Yes, place each function in a separate module. That way the linker will link in only the items needed.

FredK
  • 4,036
  • 1
  • 6
  • 11
  • thanks for the brief response. So to have independency between functions, I should make a module per function, and inside that module, only the functions needed for the main function to work should be there, is that right? – n233g16 Jan 06 '16 at 17:23
  • 1
    yes, unless some other function also needs the extra functions to work. In that case the extra function(s) should also be in a separate module. – FredK Jan 06 '16 at 17:27
  • And the principle remains, so only those extra function's code is used and not a whole useless module. Thanks a lot for the help! – n233g16 Jan 06 '16 at 17:38
1

In short, there are compiler flags to prune unused functions from the final executable code, however they are not enabled by default.

GCC can do this "garbage collection" of unused functions if these flags are added:

  1. -ffunction-sections as a compile-time flag. It instructs the compiler to create a separate section (see object file format) for each function. There's also -fdata-sections flag with similar meaning that works for variables.

  2. -Wl,--gc-sections as a link-time flag. The -Wl part instructs GCC to pass the following options to the linker. --gc-sections means "garbage select sections from which all code is unsed". Since due to the compile-time options each function has got a separate section, it effectively performs function-level pruning.

kfx
  • 6,594
  • 3
  • 23
  • 44
  • With this said, I could put both fc and fe functions in one module? Then the linker would prune the unused functions? – n233g16 Jan 06 '16 at 18:00
  • What exactly you mean by "module"? If the functions are in different libraries, sure, they can be discarded separately. If not, then the technique described above must be applied (to my knowledge). – kfx Jan 06 '16 at 18:03
  • By module I mean a ".c" file. – n233g16 Jan 06 '16 at 18:08
  • 1
    Yes, if the different .c files are compiled to different .o files, they are discarded separately, by default. – kfx Jan 06 '16 at 18:13