如何将数据结构或类从Python传递到C / C ++? [英] How to transfer data structure or class from Python to C/C++?

查看:129
本文介绍了如何将数据结构或类从Python传递到C / C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,


我是Python的新手。我想问下面的问题:


我有一个C / C ++应用程序,我想用Python作为它的扩展。

要做到这一点,我有将一些数据结构从C / C ++

应用程序转移到Python并从Python获得一些数据结构到C / C ++
应用程序。我研究过Python文档,但仅举例说明如何转移一些简单的数据,如整数,字符,

等。


你能指导我这样做吗?或者告诉我一些文件要进行

a研究?


谢谢。

解决方案

< blockquote> Hongtian:


你能指导我这样做吗?或者告诉我一些文件要研究吗?



你可以开始谷歌搜索:

- SWIG

- Boost.Python

- SIP

- ctypes(内置模块)

- 还有更多。


再见,

bearophile


10月15日,8:08 * pm,Hongtian< hongtian.i ... @ gmail.comwrote:


朋友们,


我是Python的新手。我想问下面的问题:


我有一个C / C ++应用程序,我想用Python作为它的扩展。

要做到这一点,我有将一些数据结构从C / C ++

应用程序转移到Python并从Python获得一些数据结构到C / C ++
应用程序。我研究过Python文档,但仅举例说明如何转移一些简单的数据,如整数,字符,

等。


你能指导我这样做吗?或者告诉我一些文件要研究吗?


谢谢。



拿这个是值得的。如果我理解正确,你想要

这个:


结构信息{

char * name;

char *地址;

int age;

};


int main(){

info A,B;

python_run(" \

来自urllib import urlget \ n \
来自mylib import populate_struct的
\ n \\ n

page = urlget(''http:// something'')\ n \

populate_struct(page,A)\ nn \\ b
populate_struct(页面B)\ n");

if(A.ageB.age){

something_in_C();

}

返回0;

}


我是否在正确的轨道上?你有什么问题吗?


不完全。


在我的C / C ++应用程序中,我有以下功能或流程:


void func1(....)

{

调用PyFunc(struct Tdemo,struct& Tdemo1) ;

}


我的意思是我想调用Python函数''PyFunc''并传输数据

结构'' Tdemo''这个功能。在Python中的一些过程之后,我想要它将''Tdemo1''返回到C / C ++应用程序。


我研究boost.python并思考这不是一个合理的解决方案

因为它使得C / C ++应用程序过于复杂。


谢谢。


10月16日12:09 * pm,Aaron \Castironpi \ Brady

< castiro ... @ gmail.comwrote:


10月15日,8:08 * pm,Hongtian< hongtian.i ... @ gmail.comwrote:


朋友们,


我是Python的新手。我想问下面的问题:


我有一个C / C ++应用程序,我想用Python作为它的扩展。

为此,我必须将一些数据结构从C / C ++

应用程序传输到Python,并从Python到C / C ++

应用程序获取一些数据结构。我研究过Python文档,但仅举例说明如何转移一些简单数据,如整数,字符,

等。


你能指导我这样做吗?或者告诉我一些文件要研究吗?


谢谢。



拿这个是值得的。 *如果我理解正确,你需要

这个:


结构信息{

* char * name;

* char *地址;

* int age;


};


int main( ){

* info A,B;

* python_run(" \

来自urllib import urlget \ n \ 导入populate_struct \ n \

page = urlget(''http:// something'')\ n \

populate_struct(page,A)\ n\

populate_struct(page,B)\ n");

* if(A.ageB.age){

* * something_in_C();

*}

*返回0;


}


我是否在正确的轨道上?你到目前为止有任何问题吗?


Hi friends,

I am a newer of Python. I want to ask below question:

I have a C/C++ application and I want to use Python as its extension.
To do that, I have to transfer some data structure from C/C++
application to Python and get some data structure from Python to C/C++
application. I have researched Python document, but the example only
describes how to transfer some simple data, such as integer,char,
etc.

Could you please guide me to do that? or tell me some document to have
a research?

Thanks.

解决方案

Hongtian:

Could you please guide me to do that? or tell me some document to have
a research?

You can start googling for:
- SWIG
- Boost.Python
- SIP
- ctypes (built-in module)
- And more.

Bye,
bearophile


On Oct 15, 8:08*pm, Hongtian <hongtian.i...@gmail.comwrote:

Hi friends,

I am a newer of Python. I want to ask below question:

I have a C/C++ application and I want to use Python as its extension.
To do that, I have to transfer some data structure from C/C++
application to Python and get some data structure from Python to C/C++
application. I have researched Python document, but the example only
describes how to transfer some simple data, such as integer,char,
etc.

Could you please guide me to do that? or tell me some document to have
a research?

Thanks.

Take this for what it''s worth. If I understand correctly, you want
this:

struct info {
char* name;
char* address;
int age;
};

int main( ) {
info A, B;
python_run( "\
from urllib import urlget\n\
from mylib import populate_struct\n\
page= urlget( ''http://something'' )\n\
populate_struct( page, A )\n\
populate_struct( page, B )\n" );
if( A.ageB.age ) {
something_in_C( );
}
return 0;
}

Am I on the right track? Do you have any questions so far?


Not exactly.

In my C/C++ application, I have following function or flow:

void func1(....)
{
call PyFunc(struct Tdemo, struct &Tdemo1);
}

I mean I want to invoke Python function ''PyFunc'' and transfer a data
structure ''Tdemo'' to this function. After some process in Python, I
want it return ''Tdemo1'' back to the C/C++ application.

I research boost.python and think it is not a reasonable solution
because it make the C/C++ application too complex.

Thanks.

On Oct 16, 12:09*pm, "Aaron \"Castironpi\" Brady"
<castiro...@gmail.comwrote:

On Oct 15, 8:08*pm, Hongtian <hongtian.i...@gmail.comwrote:

Hi friends,

I am a newer of Python. I want to ask below question:

I have a C/C++ application and I want to use Python as its extension.
To do that, I have to transfer some data structure from C/C++
application to Python and get some data structure from Python to C/C++
application. I have researched Python document, but the example only
describes how to transfer some simple data, such as integer,char,
etc.

Could you please guide me to do that? or tell me some document to have
a research?

Thanks.


Take this for what it''s worth. *If I understand correctly, you want
this:

struct info {
* char* name;
* char* address;
* int age;

};

int main( ) {
* info A, B;
* python_run( "\
from urllib import urlget\n\
from mylib import populate_struct\n\
page= urlget( ''http://something'')\n\
populate_struct( page, A )\n\
populate_struct( page, B )\n" );
* if( A.ageB.age ) {
* * something_in_C( );
* }
* return 0;

}

Am I on the right track? *Do you have any questions so far?


这篇关于如何将数据结构或类从Python传递到C / C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆