一、使用boost python
使用的是windows,anaconda python2.7
- include的路徑包含 \anaconda\anaconda\include,\boost\include
- lib的路徑包含 \anaconda\anaconda\libs, \boost\libs
- lib有 boost_python-vc120-mt-1_58.lib;python27.lib;
使用的是ubuntu,anaconda python2.7
- include的路徑包含 -i*\anaconda\anaconda\include\python2.7,-i*\boost\include
- lib的路徑包含 -l*\anaconda\anaconda\libs, -l*\boost\libs
- lib有 -lboost_python, -lpython2.7
執行結果:#include
#include
#include
using
namespace
std;
int main()
二、使用python 呼叫c++類
source code
makefile#include
#include
using
namespace
std;
using
namespace boost::python;
class student
string getname()
void setage(int age)
int getage()
private:
string name_;
int age_;
};boost_python_module(example)
python 端的**cc=g++
include=-i/home/yang/.pyenv/versions/anaconda2/include/python2.7
libs=-lboost_python
cflags=-fpic
example.so:student2py.o
$(cc) $(
cflags) -shared $< -o $@
$(include) $(
libs)
student2py.o:student2py.cpp
$(cc) $(
cflags) -c $< -o $@
$(include)
clean:
rm -rf student2py.o
rm -rf example.so
二、使用c++呼叫python類python端類定義** py2cplus.pyimport example
stu = example.student()
stu.setname("me")
stu.setage(23)
print stu.name
print stu.age
c++端**class
student:
def__init__
(self,name,age):
self.age = age
self.name = name
defsetname
(self,name):
self.name = name
defsetage
(self, age):
self.age = age
defgetname
(self):
return self.name
defgetage
(self):
return self.age
makefile#include
#include
#include
using
namespace
std;
using
namespace boost::python;
int main()
cc=g++
include=-i/home/yang/.pyenv/versions/anaconda2/include/python2.7
libs=-lboost_python -lpython2.7
cflags=-fpic
test2:py2cplus.cpp
$(cc) -o $@
$<
$(include) $(
libs)
boost Python 安裝和使用
專案中需要將c 的程式暴露給網路使用,我也不想使用c 來用網路程式設計,就想到可以使用python來解決web端,然後將 c 的介面暴露給 python 於是在尋求解決方案的時候找到了 boost.python 庫。boost 中的 python 庫支援在 python 和 c 之間的自由轉換,包括...
Boost Python學習筆記(二)
首先定義乙個動物類 include animal.h pragma once include class animal 其實現 如下 src animal.cpp include include animal.h animal animal std string name name name ani...
Boost Python學習筆記(三)
繼續使用前面的專案,但是先修改下python指令碼 zoo.py 新增add和str函式,分別針對整數 浮點數和字串引數的測試 def add x,y print x y def str s print output s if name main pass然後修改下main.cpp原始檔 using...