4 ms·
> An optimizing linker might be able to prove that nobody needs those definitions, and eliminate them — I personally have worked on a linker that did exactly th
by comex 6y ago
> An optimizing linker might be able to prove that nobody needs those definitions, and eliminate them — I personally have worked on a linker that did exactly that optimization — but in general your mental model of a linker should be that it won’t do that optimization.
Not by default (for some reason), but the major compiler toolchains will all do it if you pass the right flags:
- Apple: Pass -dead_strip to linker
- Linux/ELF: Pass -Wl,--gc-sections to linker and -ffunction-sections -fdata-sections to compiler
- MSVC: Pass /opt:ref to linker and /Gw /Gy to compiler
(If you're building a dynamic library you also want -fvisibility=hidden on the Unix platforms.)