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

username: xq114
cooked: <p>Hi everyone, I’m trying to extend PyTorch with C++ classes following this tutorial <a href="https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html" rel="nofollow noopener">Extending TorchScript with Custom C++ Classes</a>. I just encountered such a problem saying</p>
<pre><code class="lang-auto">python ../export_attr.py 
Traceback (most recent call last):
  File "../export_attr.py", line 3, in &lt;module&gt;
    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
</code></pre>
<p>I’m using miniconda on Ubuntu20.04 system, and I downloaded and extracted libtorch to /usr/local/src/libtorch from <a href="https://download.pytorch.org/libtorch/cu102/libtorch-cxx11-abi-shared-with-deps-1.5.0.zip" rel="nofollow noopener">libtorch-cxx11abi-cu102</a>.</p>
<p>From cmake I got the following message:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>Could someone help me with this issue? Thanks a lot!</p>

username: xq114
cooked: <p>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.</p>

username: lcxywfe
cooked: <p>Try add <code>-D_GLIBCXX_USE_CXX11_ABI=0</code> to <code>CUDA_NVCC_FLAGS</code></p>

username: flexwang
cooked: <p>Is <code>downloaded and extracted libtorch to /usr/local/src/libtorch</code> mentioned in any doc? I wonder how you know that you are supposed to do that…</p>

username: ptrblck
cooked: <p>It’s not a requirement and you can move the extracted <code>libtorch</code> package to any location you like. During the <code>cmake</code> call you would then need to specify its location via <code>-DCMAKE_PREFIX_PATH</code>.</p>