I do everything on the site Creating a C ++ extension for Python .
In the section "Converting a C ++ Project to an Extension for Python":
I can not build a project, the error is: LNK2001 for __imp__PyFloat_FromDouble, __imp__PyFloat_AsDouble, __imp__PyModule_Create2. And in the end, the error is LNK1120.
Please help me with the error!
Here is the code:
#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)); } //double tanh_impl(double x) { // return sinh_impl(x) / cosh_impl(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, the second is the C++ function name { "fast_tanh", (PyCFunction)tanh_impl, METH_O, nullptr }, // Terminate the array with an object containing nulls. { nullptr, nullptr, 0, nullptr } }; static PyModuleDef TanhBenchmark_module = { PyModuleDef_HEAD_INIT, "TanhBenchmark", // Module name "Provides some functions, but faster", // Module description 0, superfastcode_methods // Structure that defines the methods }; PyMODINIT_FUNC PyInit_TanhBenchmark() { return PyModule_Create(&TanhBenchmark_module); } Here is a screen of errors:
Note: In the section "Creating a Main C ++ Project" in paragraph 7, there are presumably two errors:
1. Forgot to specify the type of argument " x ". I wrote a double x .
2. When correcting this error, another one arises: C2169 for tanh . Possible solution: rename tanh to tanh_impl. Well, instead of sinh and cosh, write sinh_impl and cosh_impl.