Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do unused functions get optimized out?

A fairly simple question... Compilers these days tend to do a significant amount of optimizations. Do they also remove unused functions from the final output?

like image 606
Paul Manta Avatar asked Jun 02 '11 14:06

Paul Manta


2 Answers

It depends on the compiler. Visual C++ 9 can do that - unused static functions are removed at compilation phase (there's even a C4505 warning for that), unused functions with external linkage can be removed at link phase depending on linker settings.

like image 159
sharptooth Avatar answered Sep 26 '22 03:09

sharptooth


MSVC (the Visual Studio compiler/linker) can do this if you compile with /Gy and link with /OPT:REF.

GCC/binutils can do this if you compile with -ffunction-sections -fdata-sections and link with --gc-sections.

Don't know about other compilers.

like image 44
rubenvb Avatar answered Sep 22 '22 03:09

rubenvb