gpt4 book ai didi

python - 在 C++ 中创建对象并将指针传递给 python

转载 作者:行者123 更新时间:2023-11-30 03:18:35 24 4
gpt4 key购买 nike

我正在尝试使用 ctypes 与 Python 共享 C++ 对象,方法是在 C++ 中创建对象,然后通过 C 包装器将指针传递给 Python。我希望稍后能够使用 Python 类中的其他函数(以下代码中的 do_something)对该对象进行操作。

我试过下面的代码,但出现段错误。我不熟悉 C 与 C++ 以及 C 与 Python 的接口(interface),所以我不确定在传递指针时我是否在做一些根本性的错误,或者在我之后,内存是否被 Python 垃圾收集清除/转移创建对象?

This question讨论了 boost 的类似问题,但答案对 ctypes 不是很有用。

对象.h

class object {  
public:

// constructor
object() {
pointer = nullptr;
}

// destructor
virtual ~object() {
delete pointer;
pointer = nullptr;
}

// get member functions of object_pointer
// from C++
double do_something();

protected:

// pointer to the object
object_pointer *pointer;

};

extern "C" {
object* object_new();
void object_delete(object *Ob);
double object_do_something(object *Ob);
}

对象.cpp

#include "object.h"
double object::do_something() { return pointer->do_something(); }

extern "C" {
object *object_new() { return new object(); }
void object_delete(object *Ob) { delete Ob; }
double object_do_something(object *Ob) { return Ob->do_something(); }
}

对象.py
from ctypes import *

lib = cdll.LoadLibrary('./lib_object.so')
lib.object_new.argtypes = ()
lib.object_new.restype = c_void_p
lib.special_delete.argtypes = c_void_p,
lib.special_delete.restype = None
lib.object_pointer.argtypes = c_void_p
lib.object_pointer.restype = c_void_p

class Object:
def __init__(self):
self.obj = lib.object_new()
print self.obj
def __del__(self):
lib.object_delete(self.obj)
def do_something(self):
lib.object_do_something(self.obj)

s = Object()
>> 94549743086144
s.do_something()
>> Segfault

如有任何帮助,我们将不胜感激!

最佳答案

注意事项:

  • 问题中的所有文件(.cpp.py)均未编译。它们包含语法 错误以及语义 错误
  • 我不知道指针 的作用是什么(它产生了语法 错误)。我只能假设尝试了单例实现
  • 考虑到上述情况,我没有指出现有文件中的错误(有很多错误),而是创建了一个全新的基本示例
  • 虽然 ctypes 不是唯一需要改进的地方,但我也指出了这一点:[Python 2]: ctypes - A foreign function library for Python

object.h:

class Object {
public:

Object() {
m_double = 2.718282;
}

virtual ~Object() {}

double do_something();

private:

double m_double;
};


extern "C" {
Object* object_new();
void object_delete(Object *ob);
double object_do_something(Object *ob);
}

object.cpp:

#include <iostream>
#include "object.h"


using std::cout;

double Object::do_something() {
std::cout << "Doing something in C++\n";
return m_double;
}


extern "C" {
Object *object_new() { return new Object(); }
void object_delete(Object *pObj) { delete pObj; }
double object_do_something(Object *pObj) { return pObj->do_something(); }
}

object.py:

#!/usr/bin/env python2

import sys
import ctypes


lib = ctypes.cdll.LoadLibrary('./lib_object.so')

lib.object_new.argtypes = []
lib.object_new.restype = ctypes.c_void_p
lib.object_delete.argtypes = [ctypes.c_void_p]
lib.object_do_something.argtypes = [ctypes.c_void_p]
lib.object_do_something.restype = ctypes.c_double


class Object:
def __init__(self):
self.obj = lib.object_new()
print("`Object` instance (as a `void *`): 0x{:016X}".format(self.obj))

def __del__(self):
lib.object_delete(self.obj)

def do_something(self):
return lib.object_do_something(self.obj)


def main():
obj = Object()
ret = obj.do_something()
print(ret)


if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()

输出:

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054594122]> ls
object.cpp object.h object.py
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054594122]> gcc -shared -fPIC -o lib_object.so object.cpp -lstdc++
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054594122]> python2 object.py
Python 2.7.14 (default, Oct 31 2017, 21:12:13)
[GCC 6.4.0] on cygwin

`Object` instance (as a `void *`): 0x0000000600078870
Doing something in C++
2.718282

关于python - 在 C++ 中创建对象并将指针传递给 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54594122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com