安裝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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #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中的用法:
1 2 3 4 5 6 7 8 | >> 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! >>> |
Mon, 29 Jul 2013 22:44:16 +0800
看起来很好用的样子呢=w=
Tue, 13 Aug 2013 19:51:36 +0800
Lovely Boost~