Home > Backend Development > C++ > How Can I Reduce Executable Size Using GCC and ld to Strip Unused Symbols?

How Can I Reduce Executable Size Using GCC and ld to Strip Unused Symbols?

Susan Sarandon
Release: 2024-12-30 00:45:09
Original
431 people have browsed it

How Can I Reduce Executable Size Using GCC and ld to Strip Unused Symbols?

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template