1

I have a large number of includes in my C program. During development I experimented with different ways of doing things so I bet there a number of libraries that I have included but are not used.

Does the compiler get rid of libraries that are not used? Is there a tool that can tell me? Even if the compiler does get rid of the code, it would tidy up the source if I can get rid of some things.

Bart
  • 19,692
  • 7
  • 68
  • 77
ale
  • 11,636
  • 27
  • 92
  • 149
  • 3
    Normally `#include`s from the standard library just provide prototypes and declarations, i.e. all stuff that exists only at compile time, so I suppose that probably they are just slowing down the compilation; still, cleaning up the source from unnecessary include is surely a good thing. – Matteo Italia Jul 31 '12 at 15:16
  • 2
    Which toolchain are you using? Compilers won't remove dead code, but a linker could. – user7116 Jul 31 '12 at 15:16
  • 1
    check out this link: http://stackoverflow.com/questions/51561/how-do-i-automate-finding-unused-include-directives – heretolearn Jul 31 '12 at 15:24

3 Answers3

3

It's not actually the compiler that decides whats finally goes into the executable, but the linker. Modern linkers are smart enough not not pull in code from a library unless the code is used. So you can link to hundreds of libraries, but if you don't call any functions in them then they wont add any code to your program.

As for the header files, most doesn't contain anything more than declarations and pre-processor macros, and those won't add code by themselves. The biggest drawback with including many header files is that it will slow down compilation of the source file.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

No, the toolset will link in whatever libraries you told it to link in. You can run your source code through a tool such as Doxygen which will generate pages of documentation and graphs on how your code is being used, but it won't necessarily tell you whether you are making proper or efficient use of a library.

It's up to the programmer to determine if the library is necessary and to carefully select what libraries are needed to execute the program. Unnecessary includes will drive up compile time and can potentially bloat binaries.

0

The Linux utilities which might be useful for you to find the

a)included symbols (say a function) which is there in your object code/library

b)and the dependencies for the shared libraries are nm and ldd tools respectively.

The link below is quite useful for more information,

http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html