From 5a9b918081056f9f791dd6d6b11c2c0c537f64dd Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 14 Feb 2025 14:18:25 +0100 Subject: [PATCH] cmake: link with `--gc-sections` This linker flag, if supported, removes unused sections from the executable. Unused sections are those that do not contain public symbols (therefore `-fvisibility=hidden`) and are not referenced by other sections. This shrinks the code size by 4%: text data bss dec hex filename 4069421 285904 1427713 5783038 583dfe Debug/bin-native64/SphereSvrX64_release 3918858 284456 1427713 5631027 55ec33 Debug/bin-native64/SphereSvrX64_release --- cmake/CompilerFlagsBase.cmake | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmake/CompilerFlagsBase.cmake b/cmake/CompilerFlagsBase.cmake index 805c860ec..714cb41aa 100644 --- a/cmake/CompilerFlagsBase.cmake +++ b/cmake/CompilerFlagsBase.cmake @@ -1,3 +1,4 @@ +include(CheckLinkerFlag) message(STATUS "Setting base compiler flags...") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CUSTOM_C_FLAGS}") @@ -56,6 +57,11 @@ if(NOT MSVC) set(local_link_options_nondebug)#-flto) endif() + check_linker_flag(CXX -Wl,--gc-sections LINKER_HAS_GC_SECTIONS) + if(LINKER_HAS_GC_SECTIONS) + list(APPEND local_link_options_nondebug -Wl,--gc-sections) + endif() + set(custom_compile_options_release ${local_compile_options_nondebug} -flto=full