From 6f9075d511f16a8ecdbd5c3db36e6ebab786a25c Mon Sep 17 00:00:00 2001 From: Micha Kalfon Date: Sun, 23 Aug 2020 13:05:25 +0300 Subject: [PATCH] Compile internal SQLite library with -ffunction-sections When building with SQLITECPP_INTERNAL_SQLITE=ON the SQLite amalgamation source is used for generating the library. Using one big source file means all the library code will be put in a single section. When building statically linked executables the entire section will be linked even if a small portion of the library is actually used. This commit addresses this issue by setting the -ffunction-sections compiler option when building the library. As each function is placed in a section of its own the linker, when passed the --gc-sections, will throw away unused sections (functions) and reduce the executable size. --- sqlite3/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sqlite3/CMakeLists.txt b/sqlite3/CMakeLists.txt index 9cfcdd0..2fb4ac7 100644 --- a/sqlite3/CMakeLists.txt +++ b/sqlite3/CMakeLists.txt @@ -29,6 +29,11 @@ endif (SQLITE_ENABLE_JSON1) if (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")) set_target_properties(sqlite3 PROPERTIES COMPILE_FLAGS "-fPIC") + + # Put each function in its own section to allow the linker garbage + # collection to remove unused section and produced a smaller + # statically-lined executables. + target_compile_options(sqlite3 PRIVATE "-ffunction-sections") endif (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")) if (UNIX AND CMAKE_COMPILER_IS_GNUCXX)