You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/python/cpp-and-python.md
+11-11Lines changed: 11 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Working with C++ and Python in Visual Studio | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: 09/28/2017
4
+
ms.date: 1/2/20178
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology:
@@ -123,14 +123,14 @@ For more information, see [Installing Python Support for Visual Studio](installa
123
123
> Don't set the **C/C++ > Code Generation > Runtime Library** option to "Multi-threaded Debug DLL (/MDd)" even for a Debug configuration. Select the "Multi-threaded DLL (/MD)" runtime because that's what the non-debug Python binaries are built with. If you happen to set the /MDd option, you see error *C1189: Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG* when building a Debug configuration of your DLL. Furthermore, if you remove `Py_LIMITED_API` to avoid the build error, Python crashes when attempting to import the module. (The crash happens within the DLL's call to `PyModule_Create` as described later, with the output message of *Fatal Python error: PyThreadState_Get: no current thread*.)
124
124
>
125
125
> Note that the /MDd option is what's used to build the Python debug binaries (such as python_d.exe), but selecting it for an extension DLL still causes the build error with `Py_LIMITED_API`.
126
-
126
+
127
127
1. Right-click the C++ project and select **Build** to test your configurations (both Debug and Release). The `.pyd` files are located in the *solution* folder under **Debug**and**Release**, not the C++ project folder itself.
128
128
129
129
1. Add the following code to the C++ project's main `.cpp` file:
130
130
131
131
```cpp
132
132
#include <Windows.h>
133
-
#include <cmath>
133
+
#include <cmath>
134
134
135
135
const double e = 2.7182818284590452353602874713527;
136
136
@@ -149,7 +149,6 @@ For more information, see [Installing Python Support for Visual Studio](installa
149
149
150
150
1. Build the C++ project again to confirm that your code is correct.
151
151
152
-
153
152
## Convert the C++ project to an extension for Python
154
153
155
154
To make the C++DLL into an extension for Python, you need to modify the exported methods to interact with Python types. Then you need to add a function that exports the module, along with definitions of the module's methods. For background on what's shown here, refer to the [Python/C API Reference Manual](https://docs.python.org/3/c-api/index.html) and especially [Module Objects](https://docs.python.org/3/c-api/module.html) on python.org. (Remember to select your version of Python from the drop-down control on the upper right.)
@@ -170,34 +169,35 @@ To make the C++ DLL into an extension for Python, you need to modify the exporte
170
169
}
171
170
```
172
171
173
-
1. Add a structure that defines how the C++`tanh` function is presented to Python:
172
+
1. Add a structure that defines how the C++`tanh_impl` function is presented to Python:
174
173
175
174
```cpp
176
175
static PyMethodDef superfastcode_methods[] = {
177
-
// The first propertyis the name exposed to python, the second is the C++ function name
176
+
// The first propertyis the name exposed to Python, fast_tanh, the second is the C++
177
+
// function name that contains the implementation.
// Terminate the array with an object containing nulls.
181
181
{ nullptr, nullptr, 0, nullptr }
182
182
};
183
183
```
184
184
185
-
1. Add a structure that defines the module as you see it through Python code. (Filenames internal to the C++ project, like module.cpp, are inconsequential.)
185
+
1. Add a structure that defines the module as you want to refer to it in your Python code, specifically when using the `from...import` statement. In the following example, the "superfastcode" module name means you can use `from superfastcode import fast_tanh`in Python, because `fast_tanh`is defined within `superfastcode_methods`. (Filenames internal to the C++ project, like module.cpp, are inconsequential.)
186
186
187
187
```cpp
188
188
static PyModuleDef superfastcode_module = {
189
189
PyModuleDef_HEAD_INIT,
190
-
"superfastcode",// Module name asPython sees it
190
+
"superfastcode",// Module name to use withPython import statements
191
191
"Provides some functions, but faster", // Module description
192
192
0,
193
-
superfastcode_methods // Structure that defines the methods
193
+
superfastcode_methods // Structure that defines the methods of the module
194
194
};
195
195
```
196
196
197
197
1. Add a method that Python calls when it loads the module, which must be named `PyInit_<module-name>`, where *<module_name>* exactly matches the C++ Project's **General > Target Name** property (that is, it matches the filename of the `.pyd` built by the project).
198
198
199
199
```cpp
200
-
PyMODINIT_FUNC PyInit_superfastcode() {
200
+
PyMODINIT_FUNC PyInit_superfastcode() {
201
201
return PyModule_Create(&superfastcode_module);
202
202
}
203
203
```
@@ -248,7 +248,7 @@ After you've completed either of the methods above, you can now call the `fast_t
248
248
1. Add the following lines in your `.py`file to call the `fast_tanh` method exported from the DLLand display its output. If you type the `from s` statement manually, you'll see `superfastcode` come up in the completion list, and after typing `import` the `fast_tanh` method appears.
249
249
250
250
```python
251
-
from superfastcode import fast_tanh
251
+
from superfastcode import fast_tanh
252
252
test(lambdad: [fast_tanh(x) for x in d], '[fast_tanh(x) for x in d]')
0 commit comments