如何在c++;来自python? 如果您打算在C++和Python之间设计通信介质,那么我建议去组合ZMQ和谷歌协议缓冲区。
proto-buf用于序列化/反序列化,zmq用于通信介质。为了能够正确调用函数,必须用python声明函数的参数和返回类型 因此,在调用如何在c++;来自python? 如果您打算在C++和Python之间设计通信介质,那么我建议去组合ZMQ和谷歌协议缓冲区。,python,c++,ctypes,Python,C++,Ctypes,proto-buf用于序列化/反序列化,zmq用于通信介质。为了能够正确调用函数,必须用python声明函数的参数和返回类型 因此,在调用test函数之前插入以下内容: lib.test.argtypes = [SecondStructure] lib.test.restype = ctypes.c_int 在我看来,一切都会好起来的 只要C-to-python接口的数量保持“小”(不管是什么),我认为ctypes就可以了。您可能想看看Boost.python 允许您从C++编译python
test
函数之前插入以下内容:
lib.test.argtypes = [SecondStructure]
lib.test.restype = ctypes.c_int
在我看来,一切都会好起来的
只要C-to-python接口的数量保持“小”(不管是什么),我认为
ctypes
就可以了。您可能想看看Boost.python
允许您从C++编译python模块,并定义Python如何允许以易于理解的方式访问C++代码
< P> OK,我得到了,修改代码如下:#coding=utf-8
import ctypes
from ctypes import *
class FirstStructure(Structure):
_fields_ = [
("first_int", c_int),
("second_int", c_int)
]
class SecondStructure(Structure):
_fields_ = [
("third_int", c_int),
("ft", FirstStructure)
]
if __name__ == '__main__':
fs = FirstStructure(1, 2)
ss = SecondStructure(3, fs)
print ss.ft.first_int
lib = ctypes.CDLL("./array.so")
print lib.test(ss)
#include <iostream>
using namespace std;
extern "C"
{
struct FirstStructure
{
public:
int first_int;
int second_int;
};
struct SecondStructure
{
public:
int third_int;
FirstStructure ft;
};
int test_structure(SecondStructure *ss)
{
int sum = ss->ft.first_int + ss->ft.second_int + ss->third_int;
return sum;
}
int test(SecondStructure *ss)
{
return test_structure(ss);
}
}
#包括
使用名称空间std;
外部“C”
{
结构优先结构
{
公众:
int first_int;
int第二个int;
};
结构二级结构
{
公众:
第三名;
第一结构ft;
};
int test_结构(第二结构*ss)
{
整数和=ss->ft.first\u int+ss->ft.second\u int+ss->third\u int;
回报金额;
}
内部测试(第二结构*ss)
{
返回测试结构(ss);
}
}
我修好了程序错误。< /P>虽然在Windows上,我还是复制了代码,除了添加<代码> .yDeSqCuff[dLuts](/Cord>)到<代码>测试> /代码>函数,加载一个.dll而不是.dll,它工作得很好,返回<代码> 1 < /C>和<代码> 6 < /C>。