swig使python能调用C++
来源:互联网 发布:哪里有软件编程学校 编辑:程序博客网 时间:2024/04/27 16:28
总的步骤就是用swig生成相应的cxx,然后再在程序中调用python开放给C++的接口来调用python脚本。
1、C++头文件
testSWIG.h
std::string SWIGFunction();
class SWIGClass
{ public: SWIGClass( int value );
void PrintValue();
protected: int m_value; };
2、SWIG 接口文件
SWIGME.i
/* File : SWIGME.i */
%module SWIGME
%include "std_string.i"
/* Let's just grab the original header file here */
%include "testSWIG.h"
3、生成SWIGME_wrap.cxx
打开控制台,进入到前面两个文件所在的目录,输入:
swig -c++ -python SWIGME.i
成功的话就会在当前目录下生成SWIGME_wrap.cxx和SWIGME.py两个文件。
4. 动态调用C++代码的python脚本
test.py
import SWIGME s = SWIGME.SWIGFunction() print "Result of SWIGME.SWIGFunction: " + s print "Creating SWIGME.SWIGClass of 10:" a = SWIGME.SWIGClass( 10 ) a.PrintValue() print "Creating SWIGME.SWIGClass of 42:" b = SWIGME.SWIGClass( 42 ) b.PrintValue()
5、C++实现代码
SWIGME.h
#include <string>#include <iostream>class SWIGClass{public: SWIGClass( int value ); void PrintValue();protected: int m_value;};
SWIGME.cpp
#include "SWIGME.h"SWIGClass::SWIGClass( int value ) : m_value( value ){}void SWIGClass::PrintValue(){ std::cout << "My Value is " << m_value << std::endl;}
6、测试代码
test.cpp
#include "SWIGME.h"#include "SWIGME_wrap.cxx"int main() { std::cout << "Exporting C++!" << std::endl; Py_Initialize(); // initialize python // initialize SWIGME module: init_SWIGME(); // Import and run swiggy: PyRun_SimpleString( "import test" ); Py_Finalize(); // shut down python return 0;}
如果成功的话,显示结果将为
Exporting C++!Result of SWIGME.SWIGFunction: This has been brought to you by the letter CCreating SWIGME.SWIGClass of 10:My Value is 10Creating SWIGME.SWIGClass of 42:My Value is 42
- swig使python能调用C++
- Python调用C/C++(使用SWIG)
- 在Python中调用C++,使用SWIG
- centos swig 编译c,python调用
- python通过swig调用c程序举例
- 采用SWIG实现python调用C函数
- swig c python
- Python与C++,SWIG
- 利用swig实现python调用C/C++的方法
- 在Python中使用SWIG调用C和C++程序
- 通过SWIG封装C/C++代码被Python调用
- python通过swig调用c++dll时指针的用法
- 利用swig实现python调用C/C++的方法
- 利用swig实现python调用C/C++的方法
- TensorFlow中的SWIG实现Python调用C/C++代码
- 利用Swig在python中调用C/C++代码
- python调用c和c++库(直接调用和使用swig)
- swig封装c++至python可调用
- innerHTML与innerText的用法与区别
- js处理跳转的页不被IFrame嵌套,永远不被任何IFrame使用
- SSH不输入密码连接远程Linux主机
- (IIS)报错 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
- 制作可运行jar文件
- swig使python能调用C++
- YII Framework学习教程-YII的Component组件-2011-11-23
- 字符串复制
- PLSQL导出大量数据-超出excel限制,使用csv
- 23种设计模式-Java
- 文摘网址
- 软件架构师之职责范围和高级软件工程师的职责范围
- 电子秤---乱码转格式
- Android学习笔记— 事件处理机制(一)