0

私はPythonモジュールを呼び出すVisual Studio 2015 C++プロジェクトを持っています。Visual StudioのC++プロジェクトからPythonコードをデバッグする方法2015

次のマイクロソフトのWebサイトのチュートリアルでは、Visual StudioのPythonプロジェクトから呼び出されたときにC++コードをデバッグする方法を示しています。

REF:https://docs.microsoft.com/en-us/visualstudio/python/debugging-mixed-mode

同様に、呼び出し

#include "stdafx.h" 
#include <iostream> 
#ifdef _DEBUG 
#undef _DEBUG 
#include <python.h> 
#define _DEBUG 
#else 
#include <python.h> 
#endif 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    printf("Calling Python to find the sum of 2 and 2.\n"); 
    // Initialize the Python interpreter. 
    Py_Initialize(); 

    // Create some Python objects that will later be assigned values. 
    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue; 

    // Convert the file name to a Python string. 
    pName = PyUnicode_FromString("sample"); 

    // Import the file as a Python module. 
    pModule = PyImport_Import(pName); 

    // Create a dictionary for the contents of the module. 
    pDict = PyModule_GetDict(pModule); 

    // Get the add method from the dictionary. 
    pFunc = PyDict_GetItemString(pDict, "add"); 

    // Create a Python tuple to hold the arguments to the method. 
    pArgs = PyTuple_New(2); 

    // Convert 2 to a Python integer. 
    pValue = PyLong_FromLong(2); 

    // Set the Python int as the first and second arguments to the method. 
    PyTuple_SetItem(pArgs, 0, pValue); 
    PyTuple_SetItem(pArgs, 1, pValue); 

    // Call the function with the arguments. 
    PyObject* pResult = PyObject_CallObject(pFunc, pArgs); 

    // Print a message if calling the method failed. 
    if (pResult == NULL) 
     printf("Calling the add method failed.\n"); 

    // Convert the result to a long from a Python object. 
    long result = PyLong_AsLong(pResult); 

    // Destroy the Python interpreter. 
    Py_Finalize(); 

    // Print the result. 
    printf("The result is %d.\n", result); std::cin.ignore(); return 0; 

} 

を次のように私のC++コードは、例えばC++プログラム

によって呼び出されるPythonコードをデバッグすることが可能です次のようなPython 3コード

# Returns the sum of two numbers. 
def add(a, b): 
    c = a + b 
    return c 

Whileデバッグ私はC++コードに次の命令に達するにF11を押した後

c = a + b 

ので、次の命令のPythonコードにブレークポイントを置きたい、Visual Studioは、Pythonコードに飛び込む必要があります

PyObject* pResult = PyObject_CallObject(pFunc, pArgs); 

答えて

1

答えはあなたがPythonのプロジェクトがVisuaにロードされていたときに、ここで説明したよう

混合モードのデバッグのみが有効になっていることを述べてhttps://docs.microsoft.com/en-us/visualstudio/python/debugging-mixed-modeでノートとして提供されますスタジオ。そのプロジェクトは、Visual Studioのデバッグモードを決定します。これは、混在モードのオプションを使用できるようにするものです。しかし、Python.orgに記載されているようにPythonを別のアプリケーションに組み込む場合、Visual Studioは混在モードのデバッグをサポートしていないネイティブC++デバッガを使用します。

Inこの場合は、デバッグせずにC++プロジェクトを開始します(デバッグ>デバッグなしの開始またはCtrl + F5)。次に、デバッグ>プロセスにアタッチします。表示されるダイアログで、適切なプロセスを選択してから、 。ボタンを押して、次のようにPythonを選択できる「コードタイプの選択」ダイアログを開きます。「OK」を選択してダイアログを閉じ、「Attach」をクリックしてデバッガを起動します。デバッガを接続する前にデバッグするPythonを呼び出さないようにしてください。

関連する問題