使用boost python的hello tutorial

安裝BOOST: http://www.boost.org/doc/libs/1_54_0/more/getting_started/index.html

增加C++的include directory使之能找到pyconfig.h等文件。

>> export CPLUS_INCLUDE_PATH=/usr/include/python2.7/:$CPLUS_INCLUDE_PAYH

寫一個簡單的測試程序:

>> cat hello.cpp

#include <iostream>

using namespace std;

void say_hello(const char* name) {
    cout << "Hello " <<  name << "!\n";
}

int main(){return 0;}

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
    def("say_hello", say_hello);
}

編譯:
>> g++ hello.cpp -lboost_python -lpython2.7 -shared -fPIC -o hello.so
>> ls
出現有hello.so
python中的用法:

>> python
Python 2.7.3 (default, Apr 10 2013, 06:20:15)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
>>> hello.say_hello('Bill')
Hello Bill!
>>>