Optimizing executable size can be crucial in embedded systems or resource-constrained environments. To achieve this, it's important to remove unused symbols from the executable.
GCC and ld provide options to discard unused symbols during compilation and linking:
To remove unused symbols, follow these steps:
Suppose we have a file test.cpp with two functions, one of which is unused:
int use(int a); int unused(int a);
To remove the unused function, compile and link as follows:
gcc -Os -fdata-sections -ffunction-sections test.cpp -o test -Wl,--gc-sections
This command will instruct GCC to separate code and data into sections and ld to discard unreferenced sections, resulting in an executable with reduced size.
The above is the detailed content of How to Strip Unused Symbols from C/C Executables with GCC and ld?. For more information, please follow other related articles on the PHP Chinese website!