在VS代码中从Python调试C++

2024-09-23 20:24:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在Linux下调试Python中的VC++代码共享C++库调用:

<> C++代码< /p>
extern "C" void add(const int N, const double *x,const  double *y,double *z);
void add(const int N, const double *x,const  double *y,double *z)
{
    for (int i=0;i < N;i++)  
       z[i] = x[i] + y[i];  
} 

Python代码是:

import numpy as np
import ctypes
lib = ctypes.cdll.LoadLibrary("./pex.so")
lib.add.argtypes = [ctypes.c_int,
                    np.ctypeslib.ndpointer(np.float64),
                    np.ctypeslib.ndpointer(np.float64),
                    np.ctypeslib.ndpointer(np.float64)]

inp1 = np.random.randint(0,11,(10,7)).astype(np.float64)
inp2 = np.random.randint(0,11,(10,7)).astype(np.float64)
inp3 = np.zeros_like(inp2)
lib.add(inp1.size,inp1,inp2,inp3)

根据this博客,我使用了launch.json

"configurations": [
    {
        "name": "(gdb) Attach",
        "type": "cppdbg",
        "request": "attach",
        "program": "/usr/bin/python",
        "processId": "${command:pickProcess}",
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    },
    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal"
    },
   
]

由于某种原因,我无法进入add函数。结果还可以,但我无法调试代码。我做错了什么


Tags: 代码addlibnpctypesintdoublefloat64