Conflict between libtorch and grpc

zhou00009: <p>Hi, I recently tried to use grpc to deploy the model in libtorch (model is just as same as the one in <a href="https://pytorch.org/tutorials/advanced/cpp_export.html" rel="nofollow noopener">libtorch demo</a>. I had checked grpc installation and it was collectly installed (grpc version: 1.20.0) and when I compiled my project i got an error:</p>
<pre><code class="lang-auto">CMakeFiles/resnet_server.dir/resnet_server.cc.o: In function `RunServer()':
resnet_server.cc:(.text+0x11c3): undefined reference to `grpc::ServerBuilder::AddListeningPort(std::string const&amp;, std::shared_ptr&lt;grpc::ServerCredentials&gt;, int*)'
CMakeFiles/resnet_server.dir/__/protos/example.pb.cc.o:(.data.rel.ro._ZTVN7example12DataResponseE[_ZTVN7example12DataResponseE]+0x20): undefined reference to `google::protobuf::Message::GetTypeName() const'
CMakeFiles/resnet_server.dir/__/protos/example.pb.cc.o:(.data.rel.ro._ZTVN7example12DataResponseE[_ZTVN7example12DataResponseE]+0x58): undefined reference to `google::protobuf::Message::InitializationErrorString() const'
CMakeFiles/resnet_server.dir/__/protos/example.pb.cc.o:(.data.rel.ro._ZTVN7example11DataRequestE[_ZTVN7example11DataRequestE]+0x20): undefined reference to `google::protobuf::Message::GetTypeName() const'
CMakeFiles/resnet_server.dir/__/protos/example.pb.cc.o:(.data.rel.ro._ZTVN7example11DataRequestE[_ZTVN7example11DataRequestE]+0x58): undefined reference to `google::protobuf::Message::InitializationErrorString() const'
collect2: error: ld returned 1 exit status
src/CMakeFiles/resnet_server.dir/build.make:156: recipe for target 'src/resnet_server' failed
make[2]: *** [src/resnet_server] Error 1
CMakeFiles/Makefile2:85: recipe for target 'src/CMakeFiles/resnet_server.dir/all' failed
make[1]: *** [src/CMakeFiles/resnet_server.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
</code></pre>
<p>I realize that libtorch also uses its own libprotobuf.a  libprotobuf-lite.a  libprotoc.a, which may have conflict with the corresponding protbuf library of grpc.<br>
Environment:ubuntu 18.04<br>
libtorch: latest cpu version<br>
Can someone help me out and fix this? Thanks</p>

yf225: <p>There is an issue about it and it’s not fixed yet: <a href="https://github.com/pytorch/pytorch/issues/14573" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/14573</a>. Currently the easiest way is to compile libtorch with the protobuf library that grpc uses, or compile grpc with the protobuf library that libtorch uses.</p>

zhou00009: <p>I will try both your suggestions and thank you for your help, I really appreciate it.</p>

kealennieh: <p>Any updates? I encountered this problem recently.</p>

woong97: <p>I faced same issue. I’m new of cpp project.So, I don’t know how to compile libtorch with the protobuf library that grpc uses. Please, I need your help. Can you explain how to compile?</p>

wayne8l: <p>You need to manually edit the CMakeLists.txt file such that the line</p>
<pre><code class="lang-auto">option(BUILD_CUSTOM_PROTOBUF ... ON)
</code></pre>
<p>is changed to</p>
<pre><code class="lang-auto">option(BUILD_CUSTOM_PROTOBUF ... OFF)
</code></pre>
<p>This will trigger <code>find_package(protobuf)</code> instead of the included protobuf git submodule</p>
<p>Thus you also need to add towards the beginning of the CMakeLists.txt</p>
<pre><code class="lang-auto">list(APPEND CMAKE_PREFIX_PATH "{path to your protobuf install root}")
</code></pre>
<p>I’m sure there is a less invasive way of achieving the same thing but this works for me.</p>
<p>Note I also had to in command line <code>export LD_LIBRARY_PATH={path to protobuf libs}</code> before compile since the protoc binary is called at least a few times and I built grpc/protobuf using shared libraries.</p>