首先定義乙個動物類(include/animal.h)
#pragma once
#include class animal
;
其實現**如下(src/animal.cpp)
#include #include "animal.h"
animal::animal(std::string name):name_(name)
animal::~animal()
void animal::call()
void animal::move()
void animal::eat(std::string food)
#pragma once
public:
animalwrap(std::string name);
virtual ~animalwrap();
void call();
void move();
};
using namespace boost::python;
using namespace boost::python::detail;
animalwrap::animalwrap(std::string name):animal(name)
animalwrap::~animalwrap()
void animalwrap::call()
void animalwrap::move()
boost_python_module_init(boost)
最後編寫cmakelists.txt
cmake_minimum_required(version 2.8)
project(boost)
set(cmake_cxx_flags "-wall -g")
### 此處的動態庫名必須和boost_python_module()中定義的保持一致,即最後生成的庫必須名為boost.so
file(glob src "src/*.cpp")
add_library(boost shared $)
set_target_properties(boost properties prefix "")
#dependencies
include(findpkgconfig)
pkg_check_modules(python required python)
include_directories(include /usr/local/include $)
link_directories(/usr/local/lib $)
target_link_libraries(boost boost_python)
專案最終目錄結構
# tree .
.├── build
├── cmakelists.txt
├── include
│ ├── animal.h
└── src
├── animal.cpp
3 directories, 5 files
編譯
# cd build
# cmake ..
# make
編寫測試檔案(build/zoo.py)
import boost
def show():
wolf = boost.animal("wolf")
wolf.eat("beef")
goat = boost.animal("gota")
goat.eat("grass")
if __name__ == '__main__':
show()
執行測試
# cd build
# python zoo.py
wolf: eat beef
gota: eat grass
在上個專案的根目錄新增原始檔(main.cpp)
#include #include using namespace boost::python;
int main()
trycatch (const error_already_set&)
py_finalize();
return 0;
}
修改cmakelists.txt
cmake_minimum_required(version 2.8)
project(boost)
set(cmake_cxx_flags "-wall -g")
### 此處的動態庫名必須和boost_python_module()中定義的保持一致,即最後生成的庫必須名為boost.so
file(glob src "src/*.cpp")
add_library(boost shared $)
set_target_properties(boost properties prefix "")
add_executable(core main.cpp)
#dependencies
include(findpkgconfig)
pkg_check_modules(python required python)
include_directories(include /usr/local/include $)
link_directories(/usr/local/lib $)
target_link_libraries(boost boost_python)
target_link_libraries(core boost $)
編譯並執行測試
# cd build
# cmake ..
# make
# ./core
wolf: eat beef
gota: eat grass
考慮這樣乙個需求,我們要展示乙個動物園中的動物,但是動物的種類和個數都不固定,這就導致我們動物園的show
方法需要經常變動,有什麼辦法可以避免程式的反覆編譯呢?一種方式就是使用配置檔案,將需要展示的動物寫入配置檔案,然後動物園的show
方法通過解析配置檔案來生成需要展示的內容;另一種方式就是通過python指令碼來實現,因為python指令碼不需要編譯,相比於配置檔案的方式,python指令碼的方式不需要設計配置檔案格式,也不需要實現複雜的解析邏輯,使用起來更加靈活。
在上面的例子中,我們使用python指令碼實現了原本應該在main.cpp
中實現的show
方法,然後在main.cpp
中呼叫它,後面如果有改動我們直接修改python指令碼即可,無需重程式設計序。
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...
Boost Python學習筆記(一)
boost 1 66 0.tar.gz 生成編譯工具 tar axf boost 1 66 0.tar.gz cd boost 1 66 0 yum install gcc gcc c python devel cmake y bootstrap.sh編譯32位boost庫 b2 install w...
Boost python 程式設計記錄
一 使用boost python 使用的是windows,anaconda python2.7 include的路徑包含 anaconda anaconda include,boost include lib的路徑包含 anaconda anaconda libs,boost libs lib有 b...