1. 程式人生 > >Boost Python學習筆記(二)

Boost Python學習筆記(二)

通過 cmak cat 結構 固定 動物類 程序 virt 使用配置

你將學到什麽

  • 如何在Python中調用C++代碼
  • 如何在C++中調用Python代碼

在Python中調用C++代碼

首先定義一個動物類(include/animal.h)

#pragma once

#include <string>

class Animal
{
public:
  Animal(std::string name);
  virtual ~Animal();

  virtual void call();
  virtual void move();
  void eat(std::string food);

protected:
  std::string name_;
};

其實現代碼如下(src/animal.cpp)

#include <iostream>
#include "animal.h"

Animal::Animal(std::string name):name_(name)
{
}

Animal::~Animal()
{
}

void Animal::call()
{
  std::cout << name_ << ": call" << std::endl;
}

void Animal::move()
{
  std::cout << name_ << ": moving" << std::endl;
}

void Animal::eat(std::string food)
{
  std::cout << name_ << ": eat " << food << std::endl;
}

接著編寫其導出代碼(include/boost_wrapper.h)

#pragma once

#include <boost/python/wrapper.hpp>
#include "animal.h"

class AnimalWrap: public Animal, public boost::python::wrapper<Animal>
{
public:
  AnimalWrap(std::string name);
  virtual ~AnimalWrap();

  void call();
  void move();
};

其導出實現如下(src/boost_wrapper.cpp)

#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include "boost_wrapper.h"

using namespace boost::python;
using namespace boost::python::detail;

AnimalWrap::AnimalWrap(std::string name):Animal(name)
{
}

AnimalWrap::~AnimalWrap()
{
}

void AnimalWrap::call()
{
  if (override func = this->get_override("call"))
    func();
  Animal::call();
}

void AnimalWrap::move()
{
  if (override func = this->get_override("move"))
    func();
  Animal::move();
}

BOOST_PYTHON_MODULE_INIT(boost)
{
    class_<AnimalWrap, boost::noncopyable>("Animal", init<std::string>())
        .def("call", &Animal::call)
        .def("move", &Animal::move)
        .def("eat", &Animal::eat);
}

最後編寫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 ${SRC})
set_target_properties(boost PROPERTIES PREFIX "")

#dependencies
INCLUDE(FindPkgConfig)
pkg_check_modules(PYTHON REQUIRED python)
include_directories(include /usr/local/include ${PYTHON_INCLUDE_DIRS})
link_directories(/usr/local/lib ${PYTHON_LIBRARY_DIRS})
target_link_libraries(boost boost_python)

項目最終目錄結構

# tree .
.
├── build
├── CMakeLists.txt
├── include
│   ├── animal.h
│   └── boost_wrapper.h
└── src
    ├── animal.cpp
    └── boost_wrapper.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

在C++中調用Python代碼

在上個項目的根目錄添加源文件(main.cpp)

#include <iostream>
#include <boost/python.hpp>

using namespace boost::python;

int main()
{
  Py_Initialize();
  if (!Py_IsInitialized())
  {
    std::cout << "Initialize failed" << std::endl;
    return -1;
  }

  try
  {
    object sys_module = import("sys");
    str module_directory(".");
    sys_module.attr("path").attr("insert")(1, module_directory);
    object module = import("zoo");
    module.attr("show")();
  }
  catch (const error_already_set&)
  {
    PyErr_Print();
  }
  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 ${SRC})
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 ${PYTHON_INCLUDE_DIRS})
link_directories(/usr/local/lib ${PYTHON_LIBRARY_DIRS})
target_link_libraries(boost boost_python)
target_link_libraries(core boost ${PYTHON_LIBRARIES})

編譯並執行測試

# 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學習筆記(二)