### Optimized Linking

Optimized linking (compiling with /Gl or linking with /OPTFUNC) generally takes longer than regular linking. However, if the optimization is effective enough, it can actually speed up the linking process, because there is less information to write to file. The linker produces slightly faster and more compact code and files by grouping neighboring sections that have similar attributes.

As a general rule, it is best to link without the /OPTFUNC option until your code is tested and stable.

Removing Unreachable Functions
Just as the compiler can optimize your source code by removing or replacing instructions, the linker can optimize your object code, including code in libraries you are linking in, by removing unreferenced functions. When the function is removed, any code that was required only by that function is also removed, including any other functions that were referenced only by that function. This reduces the size of your output file.

Compile with the option /Gl, and then link with the option /OPTFUNC, to remove functions that are:

- Not referenced in any input file
- Rendered unreferenced by the removal of other functions
- Not exported for use in other files

If you are compiling and linking in one step, you do not need to specify /OPTFUNC. The compiler passes /OPTFUNC to the linker automatically when you specify /Gl.

Reducing the size of the executable file and improving performance

- Use /ALIGNFILE:n to set the file alignment for sections in the output file. Set n to smaller factors to reduce the size of the executable. Larger factors may reduce load time for the executable.
  By default, the alignment is set to 16.
  By default, the alignment is set to 512.
- Use /BASE:n to specify the load address for the executable. For example, if several DLLs are loaded at base addresses that ensure that the DLLs do not overlap, the linker does not have to reapply the relocation records.

> Note: Your code must be compiled with Gl+ to remove unreferenced functions.

- Specify /EXEPACK to reduce the size of the executable by compressing pages in the file. The operating system automatically decompresses the pages when the program is loaded. (/EXEPACK:2 is the best setting for building executables to run on OS/2 Version 3.0 or higher).
- Specify /PACKCODE to produce slightly faster and more compact code by grouping neighboring code segments that have similar attributes.
- Specify /PACKDATA to produce more compact files by grouping neighboring data segments that have similar attributes.
- Specify /DBGPACK when you are debugging, to reduce the size of the executable file and potentially improve debugger performance.

Linking

Summary of Linker Options