Boost.Python入門例
環境 gcc boost boost-python
コード#コード# Rectangle.cpp Rectangle2py.cpp makefile
注意事項 makeファイルのコマンドは、TAB記号で始まる でなければなりません.コンパイルおよび実行に使用するpythonヘッダファイル(Python.h)とpythonライブラリ(libpython 2.7.1 dylib)のバージョンは、 と一致する必要があります.-sharedこのオプションは、動的接続ライブラリ を生成することを指定する.-fPICは位置独立(アドレス無関係)にコンパイルされたコードを表すが、このオプションを使用しないとコンパイル後のコードは位置関係であるため、動的ロード時には、コードコピーによって異なるプロセスのニーズを満たすことができ、真のコードセグメント共有の目的 を達成することができない.-Lリンクライブラリのパス を指定-Iヘッダファイル検索パス -lxxxxはリンクライブラリの名前をxxxxと指定し、コンパイラは動的接続ライブラリを検索する際に隠された命名規則があり、すなわち与えられた名前の前にlibを付け、後に付ける.soはライブラリの名前 を決定する
転載先:https://www.cnblogs.com/Juworchey/p/8191998.html
brew install boost
brew install boost-python
コード#コード#
#include
#include
using namespace std;
class Rectangle
{
public:
void setWidth(int width)
{
width_ = width;
}
int getWidth()
{
return width_;
}
void setHeight(int height)
{
height_ = height;
}
int getHeight()
{
return height_;
}
int getArea()
{
return width_ * height_;
}
private:
int width_;
int height_;
};
#include
#include "Rectangle.cpp"
using namespace boost::python;
BOOST_PYTHON_MODULE(rectangle) //python
{
class_("Rectangle")
.def("setWidth",&Rectangle::setWidth)
.def("setHeight",&Rectangle::setHeight)
.def("getWidth",&Rectangle::getWidth)
.def("getHeight",&Rectangle::getHeight)
.def("getArea", &Rectangle::getArea)
.add_property("width",&Rectangle::getWidth,&Rectangle::setWidth)
.add_property("height",&Rectangle::getHeight,&Rectangle::setHeight)
;
}
PY_INCLUDE = /path/to/python/include/python2.7
PY_LIB = /path/to/python/lib/
rectangle.so:rectangle.o rectangle2py.o
g++ rectangle2py.o -o rectangle.so -shared -fPIC -I$(PY_INCLUDE) -L$(PY_LIB) -lpython2.7 -lboost_python
rectangle.o:
g++ -c Rectangle.cpp -o rectangle.o
rectangle2py.o: rectangle.o
g++ -c Rectangle2py.cpp -o rectangle2py.o -fPIC -I$(PY_INCLUDE)
clean:
rm -rf rectangle.o rectangle2py.o
注意事項
転載先:https://www.cnblogs.com/Juworchey/p/8191998.html