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: enter image description here 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.

  • one
    Most likely you forgot to connect the necessary libraries in the project, i.e. lib files - Alexander
  • and how to fix the error? In the properties of the project in the item added: Additional library directories: C: \ Program Files \ Python36 \ libs. - Kto To

1 answer 1

  1. Verify that Python36.lib is specified for layout, project properties: Linker> Input> Additional Dependencies
  2. Check that the C: \ Program Files \ Python36 \ libs folder actually contains the specified library.
  3. If the first two items are executed, then you need to make sure once again that the version and build of your Python36.lib is exactly the one you need.
  4. If you are sure that your Python36.lib is correct, then you need to report the error to the person from whom you took it.
  • The folder C: \ Program Files \ Python36 \ libs contains the files: "python36.lib", "python3.lib", "_tkinter.lib", "libpython36.a". There is no "Python32.lib". What then to do? .. In additional dependencies (Additional Dependencies) there is no "Python32.lib". - Kto To
  • one
    Obviously, you need to use python36.lib . - 0andriy
  • thanks, it helped. Thought that 32 denotes the bit depth - Kto To