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

I’m using clang 6.0 and I’m getting a lot of issues while linking to c10 library:

undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

All undefined references seems to be related with the std::__cxx11::basic_string.

It looks like it is something related to the C++11 ABI, has anyone faced the same issues before ?

I’m using cmake and the provided TorchConfig for CPU-only build (Linux). I also tried to build with gcc-4.8, gcc-5.5 and gcc-7.0.

Thanks !

2 Likes

How did you get the c10 library? Is it from source or from an installation?

Hi @richard, I got the binaries from here: https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip

PS: On MacOS it works fine with the libtorch MacOS library binaries.

So, it seems that I’ve found the issue. The TorchConfig.cmake has this piece of config:

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")
endif()

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 leveldb or grpc 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.

If I manually change GLIBCXX_USE_CXX11_ABI=1 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.

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.

3 Likes

@goldsborough do you know if there a reason why we’re building with -D_GLIBCXX_USE_CXX11_ABI=0?

So as far as I know, the C++ library is extracted from the Python packages.
Those, in turn, are - I think - build with “manylinux” in mind, which specifies archaic compatibility requirements.
Note that it is a variable to be filled in in the template - indeed, I seem to get =1 by default when compiling from source.

Best regards

Thomas

2 Likes

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

2 Likes

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.

1 Like

Is it possible for PyTorch to provide a built version of libtorch that is compatible with gcc >= 5.1? I also have this problem.

1 Like

WTF… finally I seems found the reason why I can not link libtorch with any other libs in ros…

I still have trouble.

I have a programe which also link libtorch with another lib compiles using gcc5, but we got no problem.

beside we also built with opencv lib which compiles also using gcc5 and new ABI. they can co-exist…

the only situation I got compile error is in ROS…

We now have new ABI binaries for libtorch. They can be found on
http://pytorch.org, or at https://github.com/pytorch/pytorch/issues/17492#issuecomment-524692441.

4 Likes

Thanks ! That will save me a lot of building time =)

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.