您的位置:首页 > 编程语言 > Python开发

基于VS2015 X64的boost.python环境搭建

2016-07-24 16:46 681 查看
1.环境介绍
    64位 win7 操作系统
    VS2015
    64位  python2.7.9 
    boost1.6.0
2.软件安装
    VS2015与python的安装,在此不再讲述。
3.boost.python动态编译库编译
   (1) 首先去boost官网(http://www.boost.org/)下载boost源码库,并将其解压到一个目录:D:\boost_1_60_0
   (2) 编译boost 的编译器 bjam.exe
        


使用VS2015提供的开发人员工具编译bjam.exe,把目录切换到D:\boost_1_60_0\tools\build,执行bootstrap.bat文件进行编译,如下图所示。



        (3)修改bjam的配置文件

在D:\boost_1_60_0\tools\build\example 目录下,用文本编辑器打开user-config.jam配置文件,这个配置文件需要修改两项一个是msvc版本,另一个python安装路径和版本。





因为VS用的是2015版本,所以msvc为14.0,并设置python的包含目录和库目录。

        (4)执行bjam.exe编译命令

    将刚才的bjam.exe 拷贝到 D:\boost_1_60_0\下,准备boost 库的编译。将命令提示符定位到 D:\boost_1_60_0\ 下
执行 bjam 编译命令。

   编译release版本(x64)

        bjam --with-python --prefix=c:\boost stage toolset=msvc-14.0 variant=release link=shared address-model=64 threading=multi runtime-link=shared install

  编译debug版本(x64)

bjam --with-python --prefix=c:\boost stage toolset=msvc-14.0 variant=debug link=shared address-model=64 threading=multi runtime-link=shared install

遇到的问题:bjam --with-python --build-type=complete          ,只能编译出静态库,动态库编译不出来。理论上能生成所有可能版本的lib (debug 动态库版,debug静态库版,release 动态库版,release静态库版,debug但线程版,debug多线程版, release单线程版,release多线程版。但是在编译时发现 编译boost python动态库版时会报python 的函数无法解析的外部符号,能成功编译boost python静态库的版本。因为64位寻址和32 位寻址的不同,所以不加address-model=64 的话会造成编译动态库版boost python 出现python 无法解析的外部符号的问题。如下图所示为x64的release和debug版本。



编译好的库下载链接:http://download.csdn.net/detail/lpp0900320123/9584814

注:

--prefix设置boost安装目录;
stage表示只生成库文件(dll与lib文件);
toolset指定编译器
variant决定编译什么版本;
link决定使用静态库还是动态库,shared是动态库,static是静态库;
address-model决定地址长度,即32还是64位程序;
threading决定使用单线程(single)还是多线程(multi)库;
runtime-link决定是静态(static)还是动态(shared)链接C/C++标准库;
install会生成包含头文件的include目录。

4.测试
在VS2015中,新建win32控制台工程,配置Release,平台为x64。并配置python和boost的包含路径和库路径。



python脚本如下:



C++代码:

#include<iostream>
#include<boost\python.hpp>
#include<Python.h>
using namespace std;
using namespace boost::python;
int main()
{
    Py_Initialize();    // 检查初始化是否成功
    if (!Py_IsInitialized())
    {
        return -1;
    }
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('./')");
    object mainModule;
    object mainNamespace;
    try
    {
        mainModule = import("__main__");
        mainNamespace = mainModule.attr("__dict__");
        //要求simple.py与可执行文件在相同路径下! 运行ok
        object simple = exec_file("simple.py", mainNamespace, mainNamespace);
        //object result = exec_file("simple.py", global, global);
        object foo = mainNamespace["foo"];
        int val = extract<int>(foo(5));
        object add = mainNamespace["add"];
        string url = extract<string>(add("abc"));
        cout << "Python has caculated add as " << val << endl;
        cout << "Python has caculated foo as " << url << endl;
    }
    catch (...)
    {
        if (PyErr_Occurred())
            PyErr_Print();
    }
    // 关闭Python
    Py_Finalize();
    system("pause");
    return 0;
}

运行结果如下图:



注:1)报运行时缺少boost_python-vc140-mt-1_60.dll动态库文件时,解决办法把boost_python-vc140-mt-1_60.dll拷贝到C:\Windows\System32目录下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ python boost