2

I am trying to deploy a C++ GRPC server on docker. Unfortunately, the grpc examples use dynamically linked GRPC libraries. When copying the executables to docker (e.g. a small alpine based one) it responds "not found". The underlying problem is that the executable depends on dynamically linked GRPC libraries.

I updated the example Makefile of GRPC to [only change is adding -static]

HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
SYSTEM ?= $(HOST_SYSTEM)
CXX = g++
CPPFLAGS += `pkg-config --cflags protobuf grpc`
CXXFLAGS += -std=c++11
ifeq ($(SYSTEM),Darwin)
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
        -lgrpc++_reflection\
        -ldl
else
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
        -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
        -ldl
endif
PROTOC = protoc
GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`

PROTOS_PATH = ../../protos

vpath %.proto $(PROTOS_PATH)

all: system-check greeter_client greeter_server greeter_async_client greeter_async_client2 greeter_async_server

greeter_client: helloworld.pb.o helloworld.grpc.pb.o greeter_client.o
    $(CXX) $^ $(LDFLAGS) -o $@ -static

However, this runs into

Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/local/lib/libgrpc.a(message_compress.o):(.text+0x43d): undefined reference to `deflateInit2_'
/usr/local/lib/libgrpc.a(message_compress.o):(.text+0x44e): undefined reference to `deflate'
/usr/local/lib/libgrpc.a(message_compress.o):(.text+0x4bb): undefined reference to `deflateEnd'
/usr/local/lib/libgrpc.a(message_compress.o):(.text+0x5bc): undefined reference to `inflateInit2_'
/usr/local/lib/libgrpc.a(message_compress.o):(.text+0x5cb): undefined reference to `inflate'
/usr/local/lib/libgrpc.a(message_compress.o):(.text+0x63b): undefined reference to `inflateEnd'

How can I adjust this to get a working statically compiled GRPC client / server?

Mike
  • 3,775
  • 8
  • 39
  • 79
  • duplicate of https://stackoverflow.com/a/20593489/4714742 ? – Jon McClung Apr 15 '19 at 14:01
  • No, because the location of the libraries is clearly specified (-L/usr/local/lib) – Mike Apr 15 '19 at 14:12
  • 1
    Rather than attempting to make a fully static build of your service, why can't you simply install whatever dependencies are missing in the docker image? – Mike Kinghan Apr 16 '19 at 07:33
  • That would probably work as well, but I am not clear exactly which dependencies I need for grpc – Mike Apr 16 '19 at 13:09
  • 1
    Adding `-static` to your linkage options requires the linker to find (and you to install) on *your* system static versions of absolutely all libraries required for the linkage, even including the C/C++ Standard libraries and compiler runtimes that are linked by default. OTOH `pkg-config --libs --static grpc++` will tell you all the dependencies of `grpc++` that you may need to install in the docker container, and that will be much the easiest option. – Mike Kinghan Apr 16 '19 at 14:20
  • I'm facing the exact same problem, is there a way to find all the libraries required by grpc? I get even more `undefined reference` messages – fedemengo Apr 25 '19 at 03:55

1 Answers1

0

You are missing link compress library, using -lz to fix this bug.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68