Do unused functions get optimized out?

C++CCompiler Construction

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

As a general rule, the answer is:

Yes: for unused static functions.

No:   for unused globally available functions.

The compiler doesn't know if some other compilation unit references it. Also, most object module types do not allow functions to be removed after compilation and also do not provide a way for the linker to tell if there exist internal references. (The linker can tell if there are external ones.) Some linkers can do it but many things work against this.

Of course, a function in its own module won't be loaded unnecessarily by any linker, unless it is part of a shared library. (Because it might be referenced in the future at runtime, obviously.)

Solution 4 - C++

With gcc if you turn on the optmizations it can remove unused functions and dead code.

More on gcc optimizations can be found here

Solution 5 - C++

Many compilers do, but it depends on the particular implementation. Debug builds will often include all functions, to allow them to be invoked or examined from within the debugger. Many embedded systems compilers, for reasons I don't totally understand(*), will include all of the functions in an object file if they include any, but will omit entirely any object files that aren't used at all.

Note that in languages which support Reflection (e.g. Java, C#, vb.net, etc.) it's possible, given the name of a function, to create a reference to it at runtime even if no references exist in the code. For example, a routine could accept a string from the console, munge it in some fashion, and generate a call to a function by that name. There would be no way for a compiler or linker to know what names might be so generated, and thus no way to know what functions may be safely omitted from the code. No such difficulty exists in C or C++, however, since there is no defined way for code to create a reference to a function, variable, or constant without an explicit reference existing in the code. Some implementations may arrange things so that consecutively-declared constants or variables will be stored consecutively, and one might thus create a reference to a later-declared one by adding an offset to an earlier-declared one, but the behavior of such tricks is explicitly not guaranteed by the C or C++ standards.

(*)I understand that it makes compiling and linking easier, but today's computers should have no trouble running more sophisticated compiling and linking algorithms than would have been practical in decades past. If nothing else, a two-pass pre-compile/pre-link/compile/link method could on the pre-compile/link phase produce a list of things that are used, and then on the "real" compile/link phase omit those that are not.

Solution 6 - C++

Quite a lot of the time, yes. Its often called linker stripping.

Solution 7 - C++

When it comes to MS it's the linker that takes care of this during link phase and the compiler might warn you about unused static functions (file scope). If you want the linker to remove unused functions you use the /OPT:REF option:

Solution 8 - C++

Under MSVC and with global functions or variable you can use __declspec( selectany ).

It will remove the function or variable if it has not being referenced in the code if the linker option /OPT:REF (Optimizations) is selected.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul MantaView Question on Stackoverflow
Solution 1 - C++sharptoothView Answer on Stackoverflow
Solution 2 - C++rubenvbView Answer on Stackoverflow
Solution 3 - C++DigitalRossView Answer on Stackoverflow
Solution 4 - C++rsachettoView Answer on Stackoverflow
Solution 5 - C++supercatView Answer on Stackoverflow
Solution 6 - C++GozView Answer on Stackoverflow
Solution 7 - C++ralphtheninjaView Answer on Stackoverflow
Solution 8 - C++handcodedView Answer on Stackoverflow