8000 Unefined reference to C10::Error::Error when linking against libTorch · Issue #13541 · pytorch/pytorch · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Unefined reference to C10::Error::Error when linking against libTorch #13541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ShahriarRezghi opened this issue Nov 3, 2018 · 15 comments
Closed
Labels
module: cpp Related to C++ API module: ios Related to iOS support - build, API, Continuous Integration, document

Comments

@ShahriarRezghi
Copy link
ShahriarRezghi commented Nov 3, 2018

🐛 Bug

Hi
I just dowloaded libTorch to work with the C++ API of pytorch. I tried to link against it in qt creator and include <torch/torch.h> but ran into an error. Here it is:

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

I thought that the problem was that I didn't link against c10 but after I did that the error still remained.

here is how I link against libTorch in my .pro file:
INCLUDEPATH += $$PWD/include
INCLUDEPATH += $$PWD/include/torch/csrc/api/include

LIBS += -L$$PWD/lib

LIBS += -ltorch -lnvrtc -lc10
LIBS += -lcudnn -lcuda -lcudart -lcublas -lcurand -lcusolver

Here $$PWD/ is the path to libTorch directory.

I know that i can use cmake but I didn't try that. I want to be able to use pytorch with qmake system because it is easier and more clean to work with.
My OS is Fedora 28 and I have CUDA 9.2 installed on my system

cc @jbschlosser

@ezyang
Copy link
Contributor
ezyang commented Nov 4, 2018

You probably have a C++ ABI problem; maybe your compiler is too old. What version of gcc are you using?

@ezyang
Copy link
Contributor
ezyang commented Nov 4, 2018

cc @goldsborough

@ShahriarRezghi
Copy link
Author

@ezyang

c++ (GCC) 8.2.1 20181011 (Red Hat 8.2.1-4)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

@zou3519 zou3519 added the module: cpp Related to C++ API label Nov 5, 2018
@goldsborough
Copy link
Contributor
goldsborough commented Nov 6, 2018

I think you're missing "-D_GLIBCXX_USE_CXX11_ABI=0" in your build flags. We provide this in our TorchConfig.cmake, which is why we recommend cmake as the easiest way to build LibTorch. That said, you can add that to your QtCreator config. See https://github.com/pytorch/pytorch/blob/master/cmake/TorchConfig.cmake.in for the relevant file

Please let me know if that fixes the issue.

@ShahriarRezghi
Copy link
Author

@goldsborough
Thank you that fixed the problem. Here is how to apply the flag for those who have the same problem:
QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0

@sky-fun
Copy link
sky-fun commented Nov 19, 2018

@goldsborough what should i do if i need to use libtorch in my project which must set "-D_GLIBCXX_USE_CXX11_ABI=1" ? anything suggestion?

@goldsborough
Copy link
Contributor

@sky-fun compile PyTorch from source

@libichong
Copy link

@goldsborough How can i use the flag in msbuild? Do you have any suggestion

@martinruenz
Copy link

@goldsborough
Similar to @sky-fun I would like to set -D_GLIBCXX_USE_CXX11_ABI=1. However, when I compile pytorch myself (-D_GLIBCXX_USE_CXX11_ABI=1 is default on my platform) I keep running into a typical ABI related error:

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

On one of the platforms I tested the error appeared at runtime, on another platform at compile-time. (Which is also kind of weird)

Is there a flag that I am missing? It seems that c10 is not build with the new ABI, whilst the rest is? Since c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) looks like a missing C++11 ABI definition.

@Kuldaen
Copy link
Kuldaen commented May 13, 2019

We are getting this problem too. It is not an abi issue. The cxx11 namespace only applies to std:: eg. std:: basic_string. Looking at libc10.so it seems this constructor is missing altogether

@lucasjinreal
Copy link

OK, I am to young to simple, sometimes naive. I tried add a flag to override like this:

set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=1")

It has no effect...... When I comment out this in libtorch Torch.cmake:

# When we build libtorch with the old GCC ABI, dependent libraries must too.
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  # set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")
endif()

It can link libtorch..........

@amin-amani
Copy link

on cmake u can use
set(CMAKE_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")

@3014214149
Copy link

I met similar error. I looked up the file "TorchConf.cmake", it said "

When we build libtorch with the old GCC ABI, dependent libraries must too.

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")
endif()
"
But I still could not link the library.
The error still exist.
/home/cbq/OpenFOAM/cbq-4.1/platforms/linux64GccDPInt32Opt/lib/libreynoldsnet.so: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)'

Could anyone help me ? Thanks a lot.

@ajinkyaambatwar
Copy link
ajinkyaambatwar commented May 5, 2021

I am trying to build custom torch functions with cuda dynamic parallelism support.As per this suggestion, I am compiling my CUDA code with the cpp code.
Basically it has three commands

nvcc -arch=sm_35 -rdc=true -c geo_query_gpu.cu -I libtorch/include/ -I ../include/ -L /usr/local/cuda-10.2/lib -lcudart -lcudadevrt

nvcc -arch=sm_35 -dlink -o geo_link.o geo_query_gpu.o -lcudadevrt -lcudart

g++ geo_query_gpu.o geo_link.o geo_query.cpp -I /libtorch/include/ -I ../include -I /libtorch/include/torch/csrc/api/include/ -I /home/ajinkya/anaconda3/envs/env/include/python3.6m/ -I /usr/local/cuda-10.2/include/ -D_GLIBCXX_USE_CXX11_ABI=0 -L /libtorch/lib/ -lc10 -lc10_cuda -ltorch_cpu -L /usr/local/cuda-10.2/lib64/ -lcudart -lcudadevrt

I am able to perform the first two commands(the nvcc commands) without error. But the g++ build returns error
undefined reference to `c10::detail::torchCheckFail and mutiple other errors related to c10::.
My cpp code has following inclusions

#include "geo_query.h"
#include "utils.h"
#include <stdio.h>

where geo_query.h has

#pragma once
#include <torch/extension.h>

The utils.h has

#pragma once
#include <ATen/cuda/CUDAContext.h>
#include <torch/extension.h>

So to build this I am needed to use libtorch and libc10.

I have set the -D_GLIBCXX_USE_CXX11_ABI=0 flag during build as can be seen above

My torch is installed from pip directly. Despite that, having the ABI issue is very counterproductive :(
Is there any way to solve this?

@wzcwd
Copy link
wzcwd commented Mar 23, 2025

I think you're missing "-D_GLIBCXX_USE_CXX11_ABI=0" in your build flags. We provide this in our TorchConfig.cmake, which is why we recommend cmake as the easiest way to build LibTorch. That said, you can add that to your QtCreator config. See https://github.com/pytorch/pytorch/blob/master/cmake/TorchConfig.cmake.in for the relevant file

Please let me know if that fixes the issue.

It works, thank you soooo much!!! @goldsborough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: cpp Related to C++ API module: ios Related to iOS support - build, API, Continuous Integration, document
Projects
None yet
Development

No branches or pull requests

0