Undefined symbol error on importing C++ extension

I built a custom C++ extension following the guidelines shown here https://pytorch.org/tutorials/advanced/cpp_extension.html. However, when I import it, I get the following error:

ImportError: /usr/local/miniconda/lib/python3.6/site-packages/zbuffertri-0.0.0-py3.6-linux-x86_64.egg/zbuffertri.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZN3c105ErrorC1ENS_14SourceLocationERKSs

I followed the discussion here which says both PyTorch and the C++ extensions should be built using -D_GLIBCXX_USE_CXX11_ABI=1 which is already the case. I get torch._C._GLIBCXX_USE_CXX11_ABI = True and my compile output does show its using D_GLIBCXX_USE_CXX11_ABI=1 while compiling the C++ extension too.

My setup.py file looks like:

from setuptools import setup
import torch
from torch.utils.cpp_extension import BuildExtension, CppExtension, CUDAExtension

setup(
    name="zbuffertri",
    ext_modules=[
        CUDAExtension(
            "zbuffertri",
            ["zbuffertri.cpp", "zbuffertri_implementation.cu"],
        )
    ],
    cmdclass={"build_ext": BuildExtension},
)

My setup is the following:
OS: Ubuntu 18.04
PyTorch: v1.1.0
GCC: v4.9
CUDA: v9.0.176
CuDNN: v7.0

Any help would be much appreciated!

1 Like

You should use GCC >= 4.9 to build PyTorch and C++ extensions.

I am using GCC 4.9 to build the C++ extensions. I didn’t build PyTorch from source but downloaded the binary PyTorch 1.0.0 from conda. Do I absolutely need to build PyTorch from source in order to be able to build C++ extensions? Nowhere in the tutorial did it say that.

Maybe you could try to install from the conda pytorch channel?

conda install pytorch=1.0.0 cuda90 -c pytorch