使用 PYBIND11_MODULE 时使用 Pybind11 将 C++ 函数集成到 Python 时出错

Error while integrating C++ function onto Python using Pybind11 while using PYBIND11_MODULE

我正在尝试集成 C++ 函数,该函数计算在这个非常有用的 link https://docs.microsoft.com/en-us/visualstudio/python/working-with-c-cpp-python-in-visual-studio?view=vs-2019

中发现的 tan 双曲函数

但是,当我尝试按照 link 中提到的步骤进行操作时,出现错误。 C++中的代码如下

#include <Windows.h>
#include <cmath>
#include <pybind11/pybind11.h>
#include <Python.h>
const double e = 2.7182818284590452353602874713527;
double sinh_impl(double x) {
    return (1 - pow(e, (-2 * x))) / (2 * pow(e, -x));
}

double cosh_impl(double x) {
return (1 + pow(e, (-2 * x))) / (2 * pow(e, -x));
}

double tanh_impl(double x) {
return sinh_impl(x) / cosh_impl(x);
}

int main() {

return 0;
}

namespace py = pybind11;

PYBIND11_MODULE(superfastcode22, m) {
m.def("fast_tanh2", &tanh_impl, R"pbdoc(
    Compute a hyperbolic tangent of a single argument expressed in 
   radians.
   )pbdoc");

#ifdef VERSION_INFO
m.attr("__version__") = VERSION_INFO;
#else
m.attr("__version__") = "dev";
#endif
}

我在尝试构建上述 C++ 代码的 .pyd 输出时在 PYBIND11_MODULE 处遇到错误。报错主要是告诉

incomplete types not allowed

我已尝试按照上述网页中提到的确切步骤进行操作。

但是,我在安装 pybind11 库时遇到了一些困难(由于超时和安装 pybind 库的访问被拒绝,我无法正确 运行 pip install pybind11),所以我只是下载了库文件并复制粘贴到 python 包含目录。这可能是原因之一吗?也许某些依赖项可能会丢失,如果我执行 pip install pybind11 我会获得这些依赖项?如果是这样,那么我可能不得不用 CPython 替换 pybind11。

根据 link 的主要错误是预处理器定义。由于某种原因,错误仍然存​​在。删除预处理器定义有帮助。

将 m.def(....) 语句修改为

m.def("fast_tanh2", &tanh_impl, "A function which finds tanh");