Link time optimization(LTO) improves the optimization effect of GCC, such as reducing binary size, increasing performance, and so on. For more details please refer to related [GCC documents](https://gcc.gnu.org/onlinedocs/gccint/LTO.html).
## Use
To use this feature, you need to include the required CMake file in your project's CMakeLists.txt after `project(XXXX)`.
```cmake
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(XXXX)
include(gcc)
```
The LTO feature is disabled by default. To use it, you should enable the option `CU_GCC_LTO_ENABLE` in menuconfig. Then specify target components or dependencies to be optimized by LTO after `include(gcc)` as follows:
So tools used to relink functions between flash and IRAM can't affect these optimized components and dependencies again. It is recommended that users had better optimize application components and dependencies than kernel and hardware driver ones.
## Example
The example applies LTO in `light` of `esp-matter` because its application code is much larger. Add LTO configuration into project script `CMakeLists.txt` as follows:
Configure `ESP32-C2` as the target platform, enable `CU_GCC_LTO_ENABLE` and `CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE`, set `COMPILER_OPTIMIZATION` to be `-Os`.
Increase the `main` task stack size to `5120` by option `ESP_MAIN_TASK_STACK_SIZE`.
Compile the project, and then you can see the firmware size decrease a lot:
Option | Firmware size | Stask cost
|:-:|:-:|:-:|
-Os | 1,113,376 | 2508
-Os + LTO | 1,020,640 | 4204
Then add `cu_gcc_string_1byte_align` after `cu_gcc_lto_set`: