How to fix C++ ABI problem for torchvision

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)

/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `torch::jit::parseSchemaOrName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/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<char, std::char_traits<char>, std::allocator<char> >, c10::optional<c10::DispatchKey>, 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<char, std::char_traits<char>, std::allocator<char> > const&, 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<char, std::char_traits<char>, std::allocator<char> >) const'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `at::from_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, c10::optional<bool>, c10::optional<long>, c10::TensorOptions)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/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<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /usr/local/torchvision0.9/lib/libtorchvision.so: undefined reference to `torch::jit::parseSchema(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
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)

I think torchvision is trying to link against c10 (libc10.so or libc10_cuda.so). Can you call ldd /usr/local/torchvision0.9/lib/libtorchvision.so ? it will normally show where are the libc10 libraries that are being linked against. Again, normally, if you built it with the setup.py module, the libc10 libraries will be those from your torch (the one used by python). Try python -c "import torch; print(torch._C._GLIBCXX_USE_CXX11_ABI)" to checkout which ABI your torch is using: it should be the old one, unless you compiled it from source.

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.

1 Like

It is very useful me. Thanks very much!

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.

Best regards

Thomas

1 Like