Python呼び出しC++

3689 ワード

1、C++コードはPythonに必要なインタフェースを提供する:
 1 #include "stdafx.h"

 2 #include <boost/python.hpp>

 3 #include <string>

 4 using namespace std;

 5 

 6 string greet()

 7 {

 8     for(int i=0;i<5;++i)

 9     {

10         return "hello";

11     }    

12 }

13 

14 BOOST_PYTHON_MODULE(hello)

15 {

16     boost::python::def("greet", greet);

17 }

注意:エクスポートモジュール名hello、接尾辞名pyd
2、Python呼び出しインタフェース
1 Python 3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32

2 Type "copyright", "credits" or "license()" for more information.

3 >>> import hello

4 >>> hello.greet()

5 'hello'

6 >>>