windows上编译boost库

说明

C++11出现前的很多“新”C++特性是在Boost库里面出现的,深度学习框架Caffe用到了Boost。编译Boost是为了解决历史遗留问题,新开发库应当直接用C++11或更高的C++标准来获得新的语言特性、库函数。

Boost使用自己的构建工具。早期叫bjam,新版叫b2。执行bootstrap命令后生成b2.exe和bjam.exe,执行哪个都一样。执行编译时是在“黑框框”(cmd命令行)中运行的,而不是在Visual Studio这样的IDE里编译,因此一旦有编译错误找起来略微麻烦。

Boost是一系列模块的组合,需要哪个子模块就编译它。例如需要filesystem模块,编译时指定--with-filesystem

基本设定

  • boost源码版本:boost1.61.0
  • 解压到F:\zhangzhuo\lib\boost-1.61.0目录
  • python:我的系统同时存在python2和python3(miniconda设定的),--with-python会很友好的编译出py2和py3的包
  • (可选)project-config.jam文件,用来配置项目中的一些设定,例如VS编译器、Python版本等
  • (可选)b2构建工具的文档:https://boostorg.github.io/build/manual/master/index.html
  • (可选)指定toolset的版本,实际上是VC的编译器toolset的版本:(来自b2文档)
Visual Studio 2019-14.2

Visual Studio 2017—14.1

Visual Studio 2015—14.0

Visual Studio 2013—12.0

Visual Studio 2012—11.0

Visual Studio 2010—10.0

Visual Studio 2008—9.0

Visual Studio 2005—8.0

Visual Studio .NET 2003—7.1

Visual Studio .NET—7.0

Visual Studio 6.0, Service Pack 5—​6.5

编译

1. bootstrap

进入目录:

f:
cd F:\zhangzhuo\lib\boost-1.61.0

执行bootstrap脚本:

bootstrap.bat

输出:

F:\zhangzhuo\lib\boost-1.61.0>bootstrap.bat
Building Boost.Build engine

Bootstrapping is done. To build, run:

    .\b2

To adjust configuration, edit 'project-config.jam'.
Further information:

    - Command line help:
    .\b2 --help

    - Getting started guide:
    http://boost.org/more/getting_started/windows.html

    - Boost.Build documentation:
    http://www.boost.org/build/doc/html/index.html

2. 修改源码(编译报错处理)

问题1:libs\python\src\converter\builtin_converters.cpp(51): error C2440: 'return': cannot convert from 'const char *' to 'void *'

我用的是boost1.61.0,解决办法:修改libs/python/src/converter/convert_to_cstring.cpp,找到convert_to_cstring()函数,修改前若干行为:

#if PY_VERSION_HEX < 0x03000000
  void* convert_to_cstring(PyObject* obj)
  {
      return PyString_Check(obj) ? PyString_AsString(obj) : 0;
  }
#elif PY_VERSION_HEX < 0x03070000
  void* convert_to_cstring(PyObject* obj)
  {
      return PyUnicode_Check(obj) ? _PyUnicode_AsString(obj) : 0;
  }
#else
  void* convert_to_cstring(PyObject* obj)
  {
      return PyUnicode_Check(obj) ? const_cast<void*>(reinterpret_cast<const void*>(_PyUnicode_AsString(obj))) : 0;
  }
#endif

参考:
https://stackoverflow.com/questions/54991157/cant-compile-boost-python-1-65-1-with-msvc2015

https://github.com/boostorg/python/commit/660487c43fde76f3e64f1cb2e644500da92fe582#diff-467cabb22a6c637452d730accca26d2e

3. 编译脚本

boost根目录新建vs2013-x64.bat,内容:

b2.exe toolset=msvc-12.0 --build-type=complete address-model=64 ^
    --with-serialization ^
    --with-system ^
    --with-date_time ^
    --with-regex ^
    --with-filesystem ^
    --with-timer ^
    --with-chrono ^
    --with-program_options ^
    --with-thread ^
    --with-python ^
    --stagedir="test"

其中--stagedir指定的目录为test,它下面有个lib目录,把编译好的dll和lib都放在这里了。

执行构建:把输出重定向到日志方便查错:

vs2013-x64.bat > log.txt 2>&2


旧版内容:

要用xx库,编译boost时就指定--with-xx。例如:

# 下载并解压boost_1.58
# 进入boost_1.58目录

bjam.exe toolset=msvc-14.0 --build-type=complete address-model=64 --with-serialization --with-system --with-date_time --with-regex --with-filesystem --with-timer  --with-chrono  --with-program_options --with-thread  --with-python --stagedir="test"

说明:

  • msvc-14.0表示visual studio 2015

  • --stagedir指定的目录为test,它下面有个lib目录,把编译好的dll和lib都放在这里了。

转载于:https://www.cnblogs.com/zjutzz/p/9475524.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值