Extending TorchScript with Custom C++ Classes: undefined symbol error

Hi everyone, I’m trying to extend PyTorch with C++ classes following this tutorial Extending TorchScript with Custom C++ Classes. I just encountered such a problem saying

python ../export_attr.py 
Traceback (most recent call last):
  File "../export_attr.py", line 3, in <module>
    torch.classes.load_library('libcustom_class.so')
  File "/home/xq114/miniconda3/lib/python3.8/site-packages/torch/_classes.py", line 46, in load_library
    torch.ops.load_library(path)
  File "/home/xq114/miniconda3/lib/python3.8/site-packages/torch/_ops.py", line 105, in load_library
    ctypes.CDLL(path)
  File "/home/xq114/miniconda3/lib/python3.8/ctypes/__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /home/xq114/Projects/Tutorials/Pytorch_torchscript_custom_classes/build/libcustom_class.so: undefined symbol: _ZN3c106ivalue14ConstantString6createENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

I’m using miniconda on Ubuntu20.04 system, and I downloaded and extracted libtorch to /usr/local/src/libtorch from libtorch-cxx11abi-cu102.

From cmake I got the following message:

cmake -DCMAKE_PREFIX_PATH=/usr/local/src/libtorch ..
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Found CUDA: /usr/local/cuda (found version "10.2") 
-- Caffe2: CUDA detected: 10.2
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 10.2
-- Found CUDNN: /usr/lib/x86_64-linux-gnu/libcudnn.so  
-- Found cuDNN: v7.6.5  (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libcudnn.so)
-- Autodetected CUDA architecture(s):  5.0
-- Added CUDA NVCC flags for: -gencode;arch=compute_50,code=sm_50
-- Found torch: /usr/local/src/libtorch/lib/libtorch.so  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xq114/Projects/Tutorials/Pytorch_torchscript_custom_classes/build

Could someone help me with this issue? Thanks a lot!

I’ve solved this by replace libtorch with a pre-cxx11abi version. I think it’s better that the version info could be specified in the tutorial.

Try add -D_GLIBCXX_USE_CXX11_ABI=0 to CUDA_NVCC_FLAGS

Is downloaded and extracted libtorch to /usr/local/src/libtorch mentioned in any doc? I wonder how you know that you are supposed to do that…

It’s not a requirement and you can move the extracted libtorch package to any location you like. During the cmake call you would then need to specify its location via -DCMAKE_PREFIX_PATH.