Optimizing Executable Size: Stripping Unused C/C Symbols
Minimizing executable size is crucial, especially in resource-constrained environments. By stripping unused symbols from an executable, it becomes leaner and more efficient in terms of memory usage. This article explores how to achieve this optimization using GCC and ld.
GCC and ld Configuration
GCC and ld can be configured to remove unused symbols from executables and libraries. To do this, two stages of compilation are required:
Example
Consider a file test.cpp which contains two declared functions, but one of them is unused. To exclude the unused function from the final executable, use the following command:
gcc -Os -fdata-sections -ffunction-sections test.cpp -o test -Wl,--gc-sections
The -Os flag instructs GCC to prioritize code size optimization.
Conclusion
By applying these configuration changes, the compiler and linker can identify and remove unused symbols, significantly reducing the size of the resulting executable. This optimization is especially valuable for embedded systems or other environments with strict resource limitations.
The above is the detailed content of How Can I Reduce C/C Executable Size by Stripping Unused Symbols?. For more information, please follow other related articles on the PHP Chinese website!