Issues linking with libtorch (C++11 ABI?)

username: christianperone
cooked: <p>I’m using clang 6.0 and I’m getting a lot of issues while linking to <code>c10</code> library:</p>
<pre><code class="lang-auto">undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &amp;gt; const&amp;amp;)'
</code></pre>
<p>All undefined references seems to be related with the <code>std::__cxx11::basic_string</code>.</p>
<p>It looks like it is something related to the C++11 ABI, has anyone faced the same issues before ?</p>
<p>I’m using cmake and the provided <code>TorchConfig</code> for CPU-only build (Linux). I also tried to build with gcc-4.8, gcc-5.5 and gcc-7.0.</p>
<p>Thanks !</p>

username: richard
cooked: <p>How did you get the c10 library? Is it from source or from an installation?</p>

username: christianperone
cooked: <p>Hi <a class="mention" href="/u/richard">@richard</a>, I got the binaries from here: <a href="https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip" rel="nofollow noopener">https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip</a></p>
<p><em>PS: On MacOS it works fine with the libtorch MacOS library binaries.</em></p>

username: christianperone
cooked: <p>So, it seems that I’ve found the issue. The <code>TorchConfig.cmake</code> has this piece of config:</p>
<pre><code class="lang-auto">if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")
endif()
</code></pre>
<p>Which forces GCC to use the old C++11 ABI. Is there any reason why libtorch is being built using the old ABI ? Because what happens is that every single dependency that I have on my project such as <code>leveldb</code> or <code>grpc</code> for instance, won’t be able to be linked because they were compiled with the newer C++11 ABI. And I guess that this is the most common situation for other projects.</p>
<p>If I manually change <code>GLIBCXX_USE_CXX11_ABI=1</code> and recompile the entire libtorch by myself, it works just fine, but that requires a lot of manual steps that aren’t quite easy to integrate into my CMake file. Not to mention that it’s really hard to figure out how to compile the libtorch correctly.</p>
<p>Are there any plans to move to the newer C++11 ABI and thus allow other dependencies to be linked without problems ? Otherwise, people will have a lot of trouble to depend on the libtorch libraries.</p>

username: richard
cooked: <p><a class="mention" href="/u/goldsborough">@goldsborough</a> do you know if there a reason why we’re building with -D_GLIBCXX_USE_CXX11_ABI=0?</p>

username: tom
cooked: <p>So as far as I know, the C++ library is extracted from the Python packages.<br>
Those, in turn, are - I think - build with “<a href="https://www.python.org/dev/peps/pep-0571/" rel="nofollow noopener">manylinux</a>” in mind, which specifies archaic compatibility requirements.<br>
Note that it is a variable to be filled in in the <a href="https://github.com/pytorch/pytorch/blob/96663edca6ff76aee37f3f603c3924d8e03abdcd/cmake/TorchConfig.cmake.in#L73" rel="nofollow noopener">template</a> - indeed, I seem to get =1 by default when compiling from source.</p>
<p>Best regards</p>
<p>Thomas</p>

username: smth
cooked: <p>we have that flag set because we build with gcc 4.9.x, which only has the old ABI.<br>
In GCC 5.1, the ABI for std::string was changed, and binaries compiling with gcc &gt;= 5.1 are not ABI-compatible with binaries build with gcc &lt; 5.1 (like pytorch) unless you set that flag.</p>

username: christianperone
cooked: <p>Thank you all for the answers ! I’ll compile the libtorch library with the new ABI set because otherwise, I’ll have to recompile all other libraries to the old ABI. I guess there is no other way to get around this dual ABI problem.</p>

username: Jonathan_Pearce
cooked: <p>Is it possible for PyTorch to provide a built version of libtorch that is compatible with gcc &gt;= 5.1?  I also have this problem.</p>

username: jinfagang
cooked: <p>WTF… finally I seems found the reason why I can not link libtorch with any other libs in ros…</p>

username: jinfagang
cooked: <p>I still have trouble.</p>
<p>I have  a programe which also link libtorch with another lib compiles using gcc5, but we got no problem.</p>
<p>beside we also built with opencv lib which compiles also using gcc5 and new ABI. they can co-exist…</p>
<p>the only situation I got compile error is in ROS…</p>

username: yf225
cooked: <p>We now have new ABI binaries for libtorch. They can be found on<br>
<a href="http://pytorch.org/" rel="nofollow noopener">http://pytorch.org</a>, or at <a href="https://github.com/pytorch/pytorch/issues/17492#issuecomment-524692441" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/17492#issuecomment-524692441</a>.</p>

username: christianperone
cooked: <p>Thanks ! That will save me a lot of building time =)</p>

username: jose
cooked: <p>Aside from downloading the new Libtorch versions, is it necessary to add something else to the CMake file? I ask because I replaced my old version with the nightly version, and I am still getting the same error when building my project.</p>

username: fabian_schutze
cooked: <p>Can it be that libtorch provided with the pip package still is contains the pre c++11 ABI? If so, what is the reason for this?</p>