How to fix C++ ABI problem for torchvision

KouWeiBin: <p>I compile torchvision0.9 from code. In the compiling process, it depended on libtorch1.8(here I used Pre-cxx11 ABI version). I have successfully compiled torchvision. but when I make it embedded in Qt, there always occurs below compiling errors.  can Any one help to solve it? (ps: I have set QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0 in my project)</p>
<pre><code class="lang-auto">/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `torch::jit::parseSchemaOrName(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `c10::demangle[abi:cxx11](char const*)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `torch::autograd::Node::name[abi:cxx11]() const'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `torch::nn::Module::named_buffers[abi:cxx11](bool) const'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `torch::Library::Library(torch::Library::Kind, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, c10::optional&lt;c10::DispatchKey&gt;, char const*, unsigned int)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `c10::Warning::warn(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, bool)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `torch::nn::Module::named_parameters[abi:cxx11](bool) const'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `c10::impl::OperatorEntry::reportSignatureError(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;) const'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `at::from_file(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, c10::optional&lt;bool&gt;, c10::optional&lt;long&gt;, c10::TensorOptions)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `torch::jit::parseSchema(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:309: Matching] Error 1
15:59:26: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Matching (kit: Desktop Qt 5.15.1 clang_64)
</code></pre>

LeviViana: <p>I think torchvision is trying to link against c10 (<code>libc10.so</code> or <code>libc10_cuda.so</code>). Can you call <code>ldd /usr/local/torchvision0.9/lib/libtorchvision.so</code> ? it will normally show where are the libc10 libraries that are being linked against. Again, normally, if you built it with the <code>setup.py</code> module, the libc10 libraries will be those from your torch  (the one used by python). Try <code>python -c "import torch; print(torch._C._GLIBCXX_USE_CXX11_ABI)"</code> to checkout which ABI your torch is using: it should be the old one, unless you compiled it from source.</p>
<p>I recently had a lot of trouble with this ABI issue. I managed to fix it by compiling torch from source (with the new ABI), and using gcc 8.4.</p>

KouWeiBin: <aside class="quote no-group" data-username="LeviViana" data-post="2" data-topic="126045">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/leviviana/48/9548_2.png" class="avatar"> LeviViana:</div>
<blockquote>
<p>python -c "import torch; print(torch._C._GLIBCXX_USE_CXX11_ABI)</p>
</blockquote>
</aside>
<p>It is very useful me. Thanks very much!</p>

tom: <aside class="quote no-group" data-username="LeviViana" data-post="2" data-topic="126045">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/leviviana/48/9548_2.png" class="avatar"> LeviViana:</div>
<blockquote>
<p>I recently had a lot of trouble with this ABI issue. I managed to fix it by compiling torch from source (with the new ABI), and using gcc 8.4.</p>
</blockquote>
</aside>
<p>This is really good advice: The old ABI is only there for compatibility with really old Python / operating system versions. So if you have the chance to recompile PyTorch, in particular if you are interested in libtorch, it is good to do so.</p>
<p>Best regards</p>
<p>Thomas</p>