Stripping Unused Symbols in GCC and ld for Code Size Optimization
Optimizing executable size is crucial, especially in embedded environments with constrained resources. This article addresses the issue of removing unused C/C symbols fromexecutables using GCC and ld.
GCC's -fdata-sections and -ffunction-sections flags instruct the compiler to separate code into distinct sections within a translation unit. This ensures that unused code, data, and functions are stored in separate sections.
Once the code is compiled, the linker removes unreferenced sections using the --gc-sections flag. This process ensures that only the utilized code is retained in the final executable. For instance, to strip unused symbols from a file called test.cpp containing two functions, one of which is unused, compile using:
gcc -Os -fdata-sections -ffunction-sections test.cpp -o test -Wl,--gc-sections
The -Os flag directs GCC to prioritize size optimization.
By incorporating these techniques into the build process, developers can dramatically reduce the size of their executables, improving loading performance in resource-constrained environments.
The above is the detailed content of How Can I Reduce Executable Size Using GCC and ld to Strip Unused Symbols?. For more information, please follow other related articles on the PHP Chinese website!