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* and macOS*: -fdata-sections -ffunction-sections -Wl, --gc-sections
Windows*: /Gy /Qoption,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.

有关编译器优化的更完整信息,请参阅优化通知