一、下載boost1.52,http://www.boost.org/。解壓文件到d:\boost\boost_1_52_0。php
二、下載python2.7.3,http://www.python.org/;(boost1.4支持到python2.5)css
三、安裝python,我安裝在了D:\Python25,環境變量設置PATH D:\Python25;html
四、開始->程序->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio 命令提示(2010)。打開一個控制檯java
五、在控制檯依次輸入如下3行:python
cd d:\boost\boost_1_49_0
bootstrap.bat
bjam stage –toolset=msvc-9.0 –with-python –stagedir=」D:\boost_1_49_0\bin\vc9」 link=static runtime-link=shared runtime-link=static threading=multi debug releaselinux
六、項目屬性中配置例如如下
連接器裏的附加庫文件夾增長。python/libs(python的安裝文件夾中),boost/vs2010/lib(生成的boost的文件夾中)ios
c/c++的附加庫文件夾增長,boost(boost的下載文件夾),python/include(python的安裝文件夾)c++
一、確認project中項目屬性中配置例如如下
連接器裏的附加庫文件夾增長,python/libs(python的安裝文件夾中),boost/vs2010/lib(生成的boost的文件夾中)
c/c++的附加庫文件夾增長,boost(boost的下載文件夾),python/include(python的安裝文件夾)web
二、假設使用的是boost.python的靜態庫:在屬性->預處理器->預處理器定義增長BOOST_PYTHON_STATIC_LIB,不然編譯的爲動態,會提示找不到python_boost*.lib什麼的算法
c++程序
// python_test.cpp : 定義控制檯應用程序的入口點。// #include "stdafx.h" #include <iostream> #include <boost/python.hpp> using namespace std; using namespace boost::python; int _tmain(int argc, _TCHAR* argv[]) { Py_Initialize(); // 初始化 object main_module = import("__main__"); object main_namespace = main_module.attr("__dict__"); // 運行多個python語句:建立一個hello.txt文本文件 str Lines = "hello = file('hello.txt', 'w')\n" "hello.write('Hello world first python!')\n" "hello.close()"; exec(Lines, main_namespace); // 運行表達式 exec("result = 5 ** 2", main_namespace); // 提取並查看變量result的值 int five_squared = extract<int>(main_namespace["result"]); // 查看變量result的值 cout << "The five_squeared caculated by python is " << five_squared << endl; //載入sys module. object sys = import("sys"); // 提取python的版本號信息 std::string version = extract<std::string>(sys.attr("version")); std::cout << version << std::endl; //要求simple.py與可運行文件在一樣路徑下! 運行ok str filename = "simple.py"; object simple = exec_file(filename, main_namespace, main_namespace); object foo = main_namespace["foo"]; int val = extract<int>(foo(5)); cout << "Python has caculated foo as " << val << endl; Py_Finalize(); cout << "My Python SUCCESS"<<endl ; system("pause"); return 0; }
在當前文件夾下創建一個simple.py的Python文件,內容例如如下:
def foo(i = 4):
return i**3
(演示樣例程序源於網絡)