Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tdlib large binary size issue #3226

Open
Lucienest opened this issue Jan 23, 2025 · 3 comments
Open

Tdlib large binary size issue #3226

Lucienest opened this issue Jan 23, 2025 · 3 comments

Comments

@Lucienest
Copy link

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?

@levlam
Copy link
Contributor

levlam commented Jan 23, 2025

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.

@Lucienest
Copy link
Author

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.

I've made changes and still the same result.
My Arch is Amd_64, Ubuntu 24.04.1 LTS (Noble Numbat)

@levlam
Copy link
Contributor

levlam commented Feb 24, 2025

Make sure that you change link and compile options before adding subdirectory. Otherwise, you changed nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants