Tdlib large binary size issue
Here's my CMakeLists.txt
cmake_minimum_required(VERSION 3.30.5)
project(myproj)
add_subdirectory(lib/td)
include_directories(lib/td)
include_directories(src/)
add_executable(myproj src/main.cpp)
set_target_properties(myproj PROPERTIES
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
add_dependencies(myproj Td::TdJsonStatic)
add_dependencies(myproj Td::TdStatic)
target_link_libraries(myproj PRIVATE Td::TdJsonStatic)
target_link_libraries(myproj PRIVATE Td::TdStatic)
if (${CMAKE_BUILD_TYPE} STREQUAL "Release")
set(DTD_ENABLE_LTO ON CACHE BOOL "DTD_LTO: Enable Link Time Optimization")
set_property(GLOBAL PROPERTY COMPILE_DEFINITIONS NDEBUG)
set_property(GLOBAL PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
add_link_options(-flto)
add_compile_options(
-O3 # Maximum optimization level
-march=native # Optimize for the host machine's CPU
-flto # Enable Link Time Optimization
-fno-math-errno # Disable setting errno after math functions
-funroll-loops # Unroll loops for better performance
-fomit-frame-pointer # Omit the frame pointer for better optimization
-ffast-math # Allow aggressive floating-point optimizations
)
endif ()
The compiled binary size is around 40MB, what am I doing wrong?
Size of the binary depends on the build options, target architecture and operating system. For example, on Android the library size without debug symbols is 12-20 MB depending on the target architecture. You are building TDLib with default options and can use -flto=thin -Oz to reduce binary size.
Also, the whole last block makes no sense, because you change compile and link options, but don't use the changed values afterwards.
Also, make sure that you don't use non-conforming to C++ standard options like -fno-math-errno or -ffast-math for third-party code, because they may break an absolutely correct code.
Size of the binary depends on the build options, target architecture and operating system. For example, on Android the library size without debug symbols is 12-20 MB depending on the target architecture. You are building TDLib with default options and can use
-flto=thin -Ozto reduce binary size.Also, the whole last block makes no sense, because you change compile and link options, but don't use the changed values afterwards.
Also, make sure that you don't use non-conforming to C++ standard options like
-fno-math-errnoor-ffast-mathfor third-party code, because they may break an absolutely correct code.
I've made changes and still the same result. My Arch is Amd_64, Ubuntu 24.04.1 LTS (Noble Numbat)
Make sure that you change link and compile options before adding subdirectory. Otherwise, you changed nothing.