c++grpc-c++

grpc example helloworld binaries size too big in linux


I installed grpc on my ubuntu 22.04 Then I build examples. I call following:

/usr/bin/cmake ../ -DCMAKE_BUILD_TYPE:STRING=MinSizeRel -G "Unix Makefiles"

and build:

/usr/bin/cmake --build /home/user/grpc/examples/cpp/helloworld/build --target all -j 8

In the end I get compiled binaries of basic client-server examples in the build folder. The size of each of them is bigger than 37MB. What is the reason?

If I change CMADE_BUILD_TYPE to Release, no difference - size is still 37MB If I change to Debug, size is more than 40 (that is OK).

Why Release versions of basic grpc example programs are so enormouse in size?

I started this investigation because have same problem with my own project (its size without grps was 10MB, and it is 45MB wtih grpc).

This is cmakelists file from the helloworld

cmake_minimum_required(VERSION 3.5.1)

project(HelloWorld C CXX)

include(../cmake/common.cmake)

# Proto file
get_filename_component(hw_proto "../../protos/helloworld.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)

# Generated sources
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h")
add_custom_command(
      OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
      COMMAND ${_PROTOBUF_PROTOC}
      ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
        --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
        -I "${hw_proto_path}"
        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
        "${hw_proto}"
      DEPENDS "${hw_proto}")

# Include generated *.pb.h files
include_directories("${CMAKE_CURRENT_BINARY_DIR}")

# hw_grpc_proto
add_library(hw_grpc_proto
  ${hw_grpc_srcs}
  ${hw_grpc_hdrs}
  ${hw_proto_srcs}
  ${hw_proto_hdrs})
target_link_libraries(hw_grpc_proto
  ${_REFLECTION}
  ${_GRPC_GRPCPP}
  ${_PROTOBUF_LIBPROTOBUF})

# Targets greeter_[async_](client|server)
foreach(_target
  greeter_client greeter_server 
  greeter_callback_client greeter_callback_server 
  greeter_async_client greeter_async_client2 greeter_async_server)
  add_executable(${_target} "${_target}.cc")
  target_link_libraries(${_target}
    hw_grpc_proto
    ${_REFLECTION}
    ${_GRPC_GRPCPP}
    ${_PROTOBUF_LIBPROTOBUF})
endforeach()

UPD. My projectg is cross platform, the source is the same. Under Windows I link statically. The exe file size is 10 times smaller, 4.5MB. So question to linux binaries gets even harder. Any way to reduce it?


Solution

  • It looks like I was able to minimize the size of binaries

    1. use -DCMAKE_BUILD_TYPE=MinSizeRel when build grpc.
    2. use same when build examples or own projects.
    3. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s") to strip the final binary

    as a result - the size is 8MB.