Cast C++ PyTorch Tensor to Python PyTorch Tensor
For a project that I am working on, I need to call from C++ a Python function, which has as input a PyTorch Tensor. While searching for a way to achieve this, I found that using a function named THPVariable_Wrap (Information I have found link 1 and link 2) could transform a C++ Pytorch Tensor to a PyObject, which can be used as input for the call to the Python function. However, I have tried importing this function by including the header file directly in my code, but this will always return the error LNK2019, when calling the function, with the following description:

Severity  Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol &quot;__declspec(dllimport) struct _object * __cdecl THPVariable_Wrap(class at::TensorBase)&quot; (_imp?THPVariable_Wrap@@YAPEAU_object@@VTensorBase@at@@@Z) referenced in function main pythonCppTorchExp   C:\Users\MyName\source\repos\pythonCppTorchExp\pythonCppTorchExp\example-app.obj    1

I believe the problem is in how I import the THPVariable_Wrap function in my C++ file. However, I am still not that skilled with C++ and the information on this is limited. Besides Pytorch, I am also using Boost for calling Python and I am using Microsoft Visual Studio 2019 (v142), with C++ 14. I posted the code I used below.
C++ File
#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;
#include &lt;boost/python.hpp&gt;
#include &lt;Python.h&gt;
#include &lt;string.h&gt;
#include &lt;fstream&gt; 
#include &lt;boost/filesystem.hpp&gt;
#include &lt;torch/torch.h&gt;

#include &lt;torch/csrc/autograd/python_variable.h&gt; /* The header file where  */

namespace python = boost::python;
namespace fs = boost::filesystem;

using namespace std;


int main() {
    string module_path = &quot;Path/to/python/folder&quot;;

    Py_Initialize();

    torch::Tensor cppTensor = torch::ones({ 100 });
    PyRun_SimpleString((&quot;import sys\nsys.path.append(\&quot;&quot; + module_path + &quot;\&quot;)&quot;).c_str());

    python::object module = python::import(&quot;tensor_test_file&quot;);
    python::object python_function = module.attr(&quot;tensor_equal&quot;);

    PyObject* castedTensor = THPVariable_Wrap(cppTensor) /* This function call creates the error.*/;

    python::handle&lt;&gt; boostHandle(castedTensor);
    python::object inputTensor(boostHandle);
    python::object result = python_function(inputTensor);

    bool succes = python::extract&lt;bool&gt;(result);
    if (succes) {
        cout &lt;&lt; &quot;The tensors match&quot; &lt;&lt; endl;
    }
    else {
        cout &lt;&lt; &quot;The tensors do not match&quot; &lt;&lt; endl;
    }
}

Python File
import torch

def tensor_equal(cppTensor):
    pyTensor = torch.ones(100)
    areEqual = cppTensor.equal(pyTensor)
    return areEqual


This is linker problem. You probably have to link libtorch.python.so. It can be located in place like /opt/conda/lib/python3.8/site-packages/torch/lib/libtorch_python.so. Or where you have your libtorch installed.
