Skip to content

Commit

Permalink
cmake: link with --gc-sections
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MaxKellermann authored and cbnolok committed Feb 15, 2025
1 parent cd16bcc commit 5a9b918
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmake/CompilerFlagsBase.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include(CheckLinkerFlag)
message(STATUS "Setting base compiler flags...")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CUSTOM_C_FLAGS}")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5a9b918

Please sign in to comment.