繁体   English   中英

为要在python中使用的c ++库生成包装器代码

[英]Generating wrapper code for a c++ library to be used in python

我正在python中创建一个仿真工具,并且需要一个c ++库。 我该如何与python接口库? 该库很大,但是我只想调用一些函数和类成员函数。

到目前为止,我已经厌倦了在较小的C ++代码上使用ctypes和SWIG,只是为了熟悉该过程。

使用ctypes时,必须已经将c ++库编译为共享库,这不是问题。 我已经使用ctypes从共享库中调用c ++函数,但是可以使用ctypes来调用类成员函数吗?

如果我使用SWIG,是否必须在接口文件中包含c ++库的所有头文件,还是仅包含包含所需功能的头文件?

ctypes可以调用C接口或extern "C" C ++接口,但是它不理解C ++特定的类,例如std::string或成员函数。

使用SWIG,您可以仅在想要公开给Python的SWIG接口文件中包含标头。 例如:

%module example

%{  
// This section is copied into the wrapper so the generated code can compile.
// No wrappers are generated from this.
#include "myheader1.h"
#include "myheader2.h"
%}

%include "myheader1.h"  // Wrap all functions directly declared in this header.    
int myfunc(int a, int b);  // Wrap only this one function in myheader2.h.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM