Libtorch with new C++11 ABI?

username: Freemanix
cooked: <p>Hi!</p>
<p>I have, as many others, problems with linking my C++ code using libtorch  with other libraries (eg. OpenCV). I understand that the problem is due to forced old C++11 ABI linkage. Is there a plan to release libtorch builds with new ABI?</p>
<p>I tried to build libtorch myself using build_libtorch.py, but the process is very cumbersome and I still got a lot of linkage problems afterwards (aka missing __fatbinwrap… etc.).</p>

username: Alani
cooked: <h2>Examples</h2>
<p>I think I meet the same problem, here is an example:</p>
<pre><code class="lang-auto">/* A.cpp */
#include &lt;iostream&gt;
#include &lt;string&gt;
using UString = std::u32string;
void foo(const UString &amp;ustr) {
     std::cout &lt;&lt; "hello" &lt;&lt; '\n';
}
int main () {
    UString ustr = U"xxxx";
    foo(ustr);
}
</code></pre>
<pre><code class="lang-auto"># CMakeLists.txt for A.cpp
project(first_example)
set(CMAKE_CXX_STANDARD 17)
add_library(B B.cpp)
target_link_libraries(B)
</code></pre>
<p>Symbol of <code>foo</code> in <code>libA.a</code> is:</p>
<pre><code class="lang-bash">$ nm --demangle libA.a | grep foo
...
0000000000000000 T foo(std::__cxx11::basic_string&lt;char32_t, std::char_traits&lt;char32_t&gt;, std::allocator&lt;char32_t&gt; &gt; const&amp;)
</code></pre>
<p><code>std::__cxx11::basic_string</code> is a new version of <code>std::basic_string</code>, where the introduced namespace <code>__cxx11</code> <a href="https://gcc.gnu.org/onlinedocs/gcc-5.2.0/libstdc++/manual/manual/using_dual_abi.html" rel="nofollow noopener">is designed for  backwards compatibility</a>. When <code>libtorch</code> is included, it forces  the  project to link to the old version of standard library.</p>
<pre><code class="lang-auto">/* B.cpp */
#include &lt;iostream&gt;
#include &lt;string&gt;
using UString = std::u32string;
void foo(const UString &amp;ustr) {
    std::cout &lt;&lt; "hello" &lt;&lt; '\n';
}
int main () {
    UString ustr = U"xxxx";
    foo(ustr);
}
</code></pre>
<pre><code class="lang-auto"># CMakeLists.txt for B.cpp
project(first_example)
set(CMAKE_CXX_STANDARD 17)
find_package(Torch REQUIRED)
add_library(A A.cpp)
target_link_libraries(first "${TORCH_LIBRARIES}")
</code></pre>
<p>After building <code>libB.a</code>, the symbol of <code>foo</code> is:</p>
<pre><code class="lang-bash">$ nm --demangle libB.a | grep foo
...
0000000000000301 T foo(std::basic_string&lt;char32_t, std::char_traits&lt;char32_t&gt;, std::allocator&lt;char32_t&gt; &gt; const&amp;)
</code></pre>
<p>If other libraries the project relied on are linked to <code>std::__cxx11::</code> and the project is forced to link to old version, there will be tons of <code>undefined reference</code> errors. Is there any way to solve this problem with  lower cost ?</p>
<h2>Environment</h2>
<ul>
<li>Ubuntu 16.04</li>
<li>g++-7</li>
<li>libtorch download from <a href="https://pytorch.org/" rel="nofollow noopener">https://pytorch.org/</a>
</li>
</ul>
<h2>Note</h2>
<p>If there is anything wrong, please feel free to edit.</p>

username: mhubii
cooked: <p>Hey there,</p>
<p>can you try to build libtorch from source with Anaconda? The process is described <a href="https://discuss.pytorch.org/t/build-against-anaconda-guide/40478">here</a>. Does this solve the problem?</p>

username: VisionZQ
cooked: <p>I had build libtorch from source based on /tools/build_libtorch.py.<br>
But, they are slower than official version.<br>
Are you meet this issue?</p>

username: Freemanix
cooked: <p>You can use my Docker image to build libtorch C++ only files with MKL support.</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/freemanix/torchbuilder" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars1.githubusercontent.com/u/46109266?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/freemanix/torchbuilder" target="_blank" rel="nofollow noopener">freemanix/torchbuilder</a></h3>

<p>Docker to build libtorch C++ libraries with modern ABI - freemanix/torchbuilder</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Build the image and then use package.sh to create libtorch binary redistributable with similar structure as official nightly Torch builds.</p>

username: Alani
cooked: <p>Thanks, this works for me ~</p>

username: EsdeathYZH
cooked: <p>I meet this issue too</p>

username: yf225
cooked: <p>We have a Github issue tracking this: <a href="https://github.com/pytorch/pytorch/issues/17492" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/17492</a>.</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: dambo
cooked: <p>I have integrated the new ABI lib in my Docker file which also encapsulates the correct OpenCV version so that the notorious <code>cv::imread()</code> method works. I worked days and nights on attempting to compile libtorch + OpenCV from scratch with or without the -DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=0 flag.</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/QuantScientist/DarkTorch" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars1.githubusercontent.com/u/18743986?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/QuantScientist/DarkTorch" target="_blank" rel="nofollow noopener">QuantScientist/DarkTorch</a></h3>

<p>A low-latency deep learning inference server using pytorch C++ frontend &amp; NVIDIA GPUs.  - QuantScientist/DarkTorch</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>