How do I link in all of PyTorch with a pybind11 .so?
I have a pybind11 c++ project which uses the pytorch c++ api:
#include &lt;pybind11/pybind11.h&gt;
#include &lt;pybind11/numpy.h&gt;
#include &lt;math.h&gt;
#include &lt;torch/torch.h&gt;

...

void f()
{
...
   torch::Tensor dynamic_parameters = torch::full({1}, /*value=*/0.5, torch::dtype(torch::kFloat64).requires_grad(true));
   torch::optim::SGD optimizer({dynamic_parameters}, /*lr=*/0.01);
...
}

PYBIND11_MODULE(reson8, m)
{
    m.def(&quot;my_function&quot;, &amp;my_function, &quot;&quot;);
}

I use distutils to compile this into a .so that can be imported in Python:
from distutils.core import setup, Extension

def configuration(parent_package='', top_path=None):
      import numpy
      from numpy.distutils.misc_util import Configuration
      from numpy.distutils.misc_util import get_info

      #Necessary for the half-float d-type.
      info = get_info('npymath')

      config = Configuration('',
                             parent_package,
                             top_path)
      config.add_extension('reson8',
                           ['reson8.cpp'],
                           extra_info=info,
                           include_dirs=[&quot;/home/ian/anaconda3/lib/python3.7/site-packages/pybind11/include&quot;,
                                          &quot;/home/ian/anaconda3/lib/python3.8/site-packages/pybind11/include&quot;,
                                          &quot;/home/ian/dev/hedgey/Engine/lib/libtorch/include&quot;,
                                          &quot;/home/ian/dev/hedgey/Engine/lib/libtorch/include/torch/csrc/api/include&quot;])

      return config


if __name__ == &quot;__main__&quot;:
      from numpy.distutils.core import setup
      setup(configuration=configuration)

It compiles with no error, but on running &quot;import reson8&quot; in python I get this error:
importerror: undefined symbol: _ZTVN5torch5optim9OptimizerE

I'm unsure if it's whether pytorch hasn't been linked into my so (although the .so is 10mb which is rather large if it doesn't include pytorch, but maybe all pybind11 .so files are large.)
How can I solve this issue?

I eventually found I needed to use the Anaconda version of torchlib rather than my own, as well as using Torch's CppExtension.  Here is my working setup.py:
from distutils.core import setup, Extension
from torch.utils.cpp_extension import BuildExtension, CppExtension

def configuration(parent_package='', top_path=None):
      import numpy
      from numpy.distutils.misc_util import Configuration
      from numpy.distutils.misc_util import get_info

      #Necessary for the half-float d-type.
      info = get_info('npymath')

      config = Configuration('',
                             parent_package,
                             top_path)

      config.ext_modules.append(CppExtension(
                name='reson8',
                sources=['reson8.cpp'],
                extra_info=info,
                extra_compile_args=['-g', '-D_GLIBCXX_USE_CXX11_ABI=0'],
                extra_ldflags=['-ltorch_python'],
                include_dirs=[&quot;/home/ian/anaconda3/lib/python3.7/site-packages/pybind11/include&quot;,
                                          &quot;/home/ian/anaconda3/lib/python3.8/site-packages/pybind11/include&quot;,
                                          &quot;/home/ian/anaconda3/lib&quot;
                                          ]
                                ))

      return config


if __name__ == &quot;__main__&quot;:
      from numpy.distutils.core import setup
      setup(configuration=configuration)

