# Exclude Unused Code and Data from the Executable

Programs often contain dead code or data that is not used during their execution. Even if no expensive whole-program inter-procedural analysis is made at compile time to identify dead code, there are compiler options you can specify to eliminate unused functions and data at link time.

This method is often referred to as function-level linking.

Options to specify:

Linux*: | -fdata-sections -ffunction-sections -Wl, --gc-sections
Windows*: | /Gy /link /OPT:REF

Some of the options in the above specifications are passed to the linker.

Advantages of this method: | Only the code that is referenced remains in an executable. Dead functions and data are stripped from the executable.
Disadvantages of this method: | The object codes may become slightly larger because each function or datum is put into a separate section. The overhead is eliminated at the linking stage. This method requires linker support to strip unused sections. This method can slightly increase linking time.

Parent topic: Methods to Optimize Code Size