无法编译Microsoft示例CppAndPython

时间:2019-02-15 21:01:01

标签: python c visual-studio

我已经开始研究C ++和Python,我对这两种语言都是陌生的,并且正努力理解MS给出的示例为何不起作用。

在下面的代码中PyModuleDef无法识别,但我不知道为什么。

#include <Python.h>
#include <Windows.h>
#include <cmath>

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));
}

PyObject* tanh_impl(PyObject *, PyObject* o) {
double x = PyFloat_AsDouble(o);
double tanh_x = sinh_impl(x) / cosh_impl(x);
return PyFloat_FromDouble(tanh_x);
}

static PyMethodDef superfastcode_methods[] = {
// The first property is the name exposed to Python, fast_tanh, the second is the C++
// function name that contains the implementation.
{ "fast_tanh", (PyCFunction)tanh_impl, METH_O, nullptr },

// Terminate the array with an object containing nulls.
{ nullptr, nullptr, 0, nullptr }
};

static PyModuleDef superfastcode_module = {
PyModuleDef_HEAD_INIT,
"superfastcode",                        // Module name to use with        Python import statements
"Provides some functions, but faster",  // Module description
0,
superfastcode_methods                   // Structure that defines the            methods of the module
};

PyMODINIT_FUNC PyInit_superfastcode() {
    return PyModule_Create(&superfastcode_module);
}

1 个答案:

答案 0 :(得分:0)

大概是针对过时的Python 2及其CASE WHEN IFNULL(condition, 0) = 0 THEN 1 ELSE condition END 进行编译。 <Python.h> PyModuleDef appeared in Python 3


解决方案是确保您针对Python 3进行编译和链接。