python调用c++ 函数
2013-06-05 08:07
507 查看
一、环境搭建
1.安装boost库,配置环境变量D:\boost_1_52_0;D:\boost_1_52_0\stage\lib;
2.修改C:\Users\Administrator\AppData\Local\Microsoft\MSBuild\v4.0下的Microsoft.Cpp.Win32.user.props及Microsoft.Cpp.x64.user.props文件
在<IncludePath>D:\boost_1_52_0\;</IncludePath>
在<LibraryPath>D:\boost_1_52_0\stage\lib;</LibraryPath>
3.安装python配置环境变量D:\Python27\include;
2.修改C:\Users\Administrator\AppData\Local\Microsoft\MSBuild\v4.0下的Microsoft.Cpp.Win32.user.props及Microsoft.Cpp.x64.user.props文件
在<IncludePath>节点处添加D:\boost_1_52_0\;
在<LibraryPath>节点处添加D:\boost_1_52_0\stage\lib;
二、代码示例
创建dll工程
=====================================
hello.cpp
#include <iostream>
using namespace std;
class hello {
public:
string hestr;
private:
string title;
public:
hello(string str){
this->title = str;
}
string get() {
return this->title;
}
};
==========================================
hc.h
#pragma once
#include "hello.cpp"
class hc:public hello{
public:
hc(string str);
~hc(void);
int add(int a,int b);
};
=============================================
#include "hc.h"
hc::hc(string str):hello(str){
}
hc::~hc(void){
}
int hc::add(int a,int b){
return a+b;
}
========================
pyhello.cpp 连接桥梁
#include "hc.h"
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
class_<hello>("hello",init<std::string>())
.def("get",&hello::get)
.def_readwrite("hestr",&hello::hestr);
class_<hc,bases<hello>>("hc",init<std::string>())
.def("add",&hc::add);
}
编译生成dll
将生成的dll文件 修改为hello_ext.pyd
===========================================
python调用
import hello_ext
he=hello_ext.hello("testddd")
print he.get()
he.hestr="ggggddd"
print he.hestr
te=hello_ext.hc("ffff")
print te.get()
te.hestr="ddffg"
print te.hestr
print te.add(33,44)
运行结果
本文出自 “享受编程” 博客,请务必保留此出处http://demidroid.blog.51cto.com/2954092/1216346
1.安装boost库,配置环境变量D:\boost_1_52_0;D:\boost_1_52_0\stage\lib;
2.修改C:\Users\Administrator\AppData\Local\Microsoft\MSBuild\v4.0下的Microsoft.Cpp.Win32.user.props及Microsoft.Cpp.x64.user.props文件
在<IncludePath>D:\boost_1_52_0\;</IncludePath>
在<LibraryPath>D:\boost_1_52_0\stage\lib;</LibraryPath>
3.安装python配置环境变量D:\Python27\include;
2.修改C:\Users\Administrator\AppData\Local\Microsoft\MSBuild\v4.0下的Microsoft.Cpp.Win32.user.props及Microsoft.Cpp.x64.user.props文件
在<IncludePath>节点处添加D:\boost_1_52_0\;
在<LibraryPath>节点处添加D:\boost_1_52_0\stage\lib;
二、代码示例
创建dll工程
=====================================
hello.cpp
#include <iostream>
using namespace std;
class hello {
public:
string hestr;
private:
string title;
public:
hello(string str){
this->title = str;
}
string get() {
return this->title;
}
};
==========================================
hc.h
#pragma once
#include "hello.cpp"
class hc:public hello{
public:
hc(string str);
~hc(void);
int add(int a,int b);
};
=============================================
#include "hc.h"
hc::hc(string str):hello(str){
}
hc::~hc(void){
}
int hc::add(int a,int b){
return a+b;
}
========================
pyhello.cpp 连接桥梁
#include "hc.h"
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
class_<hello>("hello",init<std::string>())
.def("get",&hello::get)
.def_readwrite("hestr",&hello::hestr);
class_<hc,bases<hello>>("hc",init<std::string>())
.def("add",&hc::add);
}
编译生成dll
将生成的dll文件 修改为hello_ext.pyd
===========================================
python调用
import hello_ext
he=hello_ext.hello("testddd")
print he.get()
he.hestr="ggggddd"
print he.hestr
te=hello_ext.hc("ffff")
print te.get()
te.hestr="ddffg"
print te.hestr
print te.add(33,44)
运行结果
本文出自 “享受编程” 博客,请务必保留此出处http://demidroid.blog.51cto.com/2954092/1216346
相关文章推荐
- C++程序调用Python的函数(简单应用)及Ubuntu16.04下codeblocks的环境配置
- c++调用python函数
- C++调用 python 函数及返回值的处理【元组,字符串...】
- C++调用Python函数
- python 中调用c++定义的函数
- 初始化函数中的虚函数调用( C++ vs python )
- c++ 调用 python 函数中文返回值乱码
- C++中调用python函数
- C++中调用python函数
- python嵌入C++------ boost.python如何在C++中调用含有不定长参数tuple变量和关键字参数dict变量的函数
- Python中调用C++函数
- Python调用C++函数(SWIG,VS2013使用numpy.i完成Numpy与C++数组转换)
- 导出 C++ 函数模块给 Python3 调用
- C++与Python的混合编程-环境配置与调用无参函数
- C++调用python,直接调用模块内函数,调用类内函数
- c++调用python函数接口
- C++调用Python函数并传入传出参数
- c++调用python函数时,使用PyArray_SimpleNewFromData(nd, dims, typenum, data)函数时出现内存错误的问题
- c++调用python函数接口
- C++调用Python函数