Boost编译安装记录

From: http://www.hn1c.com/diannao/dn77110/

这个不是官方的安装手册,请在安装前参考官方安装手册。
这只是个人在MinGW(gcc 4)安装Boost 1.43.0和用VC2008安装Boost-CMake 1.41.0的备忘记录,仅供自己参考。
虽然把大部分库和Boost.Python弄明白,但还没解决一些编译错误,需要继续研究和学习=_=
我不会Boost编程,但我知道Boost的强大,例如日本有一本书
http://www.sbcr.jp/products/4797347623.html
提及到用Boost实现了游戏脚本引擎。

?

?

?


一、使用mingw从源码中编译boost库(除Boost.Python等特殊库)
1. 下载并安装msys
如果喜欢exe安装版的可以去
http://sourceforge.net/projects/mingw/files/MSYS/BaseSystem/msys-core/msys-1.0.11/
下载MSYS-1.0.11.exe
最新版的msys没有专门的安装程序,需要逐个下载然后用7zip解压。
http://sourceforge.net/projects/mingw/files/MSYS/BaseSystem/msys-core/
而且广义的msys还包括其他exe,参考
http://www.mingw.org/wiki/MSYS
个人喜好是只装最简单的msys,到需要的时候再去下载,例如MSYS DTK。

?

2. 下载并安装mingw
mingw有多个版本,例如

?

1)官方版本需要在线下载(直接下载那里标识的exe,默认安装的gcc 3)
http://sourceforge.net/projects/mingw/
虽然gcc版本比较低,但好处是有很多已经编译好的mingw移植库,可以直接下载,不需要另行编译。
http://sourceforge.net/projects/mingw/files


2)TDM's GCC/MinGW32 Builds,这个版本的gcc会比较新(默认gcc 4),适合一些高版本gcc要求的编译。
而且支持离线安装,支持64bit(需要下载另一个exe安装文件),如果不喜欢低版本的gcc可以优先用这个。
http://sourceforge.net/projects/tdm-gcc/


3)msys-cn。这个版本的gcc也比较高(gcc 4),提供一些预编译的库。
http://code.google.com/p/msys-cn/

?

虽然个人比较喜欢gcc 3,不过为了减少编译问题(每个版本都会给出测试的编译器版本,例如
http://www.boost.org/users/download/version_1_43_0)
所以还是下载了TDM's GCC/MinGW32 Builds的版本。
下载安装后把C:\mingw32下的所有内容整个剪切到msys里的mingw文件夹下,
并且保证/etc/fstab的目录映射中没有/mingw的映射,从而实现mingw和msys的整合和绿色化。
把它拷贝到一个单独的地方,例如我把它剪切到
D:\software\mingwdev\msys-1.0.10\1.0,最好不要带中文或者空格

?

3. 下载boost的源代码
一般boost的首页会给出boost的下载
http://www.boost.org/
实际上是链接到sourceforge上,为了节约时间,我下载7z后缀的文件(我的winrar支持7z)
boost_1_43_0.7z(注意要点进链接再下载)
下载完后解压到msys的home目录,例如我是
D:\software\mingwdev\msys-1.0.10\1.0\home\Administrator\boost_1_43_0

?


4. 阅读自述文档。
通常目录下会有个叫index.htm的文件,双击打开,
进入Getting Started,在末尾处(隐藏得真深入=_=)有个链接(为什么放在windows下面,囧)
Getting Started on Unix variants (e.g. Linux, MacOS)
里面会逐步教你怎么装boost,不过我觉得里面讲得有点问题(对于mingw而言)。

?


5. 构建bjam
bjam的作用类似make(个人觉得),也就是说bjam读取.jam(类似make读取Makefile)间接调用gcc工具来编译工程。官方推荐单独下载bjam的预编译版本,但后来可以直接编译。
bjam.exe的生成有两种办法:
方法一:boost根目录下有个bootstrap.sh文件,在msys命令行下输入
bootstrap.sh --with-toolset=mingw
如果编译成功,根目录下会增加以下文件
bjam.exe
bootstrap.log
project-config.jam
方法二:在tools/jam/src/执行build.sh

cd tools/jam/src 
build.sh

?
然后把tools\jam\src\bin.ntx86\bjam.exe复制到boost根目录下
(但是注意,没有生成project-config.jam)

?


6. 修改project-config.jam
如果在boost根目录有bjam.exe和project-config.jam,可以直接执行bjam

./bjam

?
但是会提示说

?

mingw.jam: No such file or directory 
...

?

那是因为boost发布包里没有把mingw当作单独的toolset,所以没有带mingw.jam
(toolset的定义可以参考boost的手册,例如我要使用gcc编译boost的源代码,toolset就是gcc)
这个时候只能修改project-config.jam,把project-config.jam中的mingw全部替换成gcc

原来的project-config.jam

# Boost.Build Configuration# Automatically generated by bootstrap.shimport option ;import feature ;# Compiler configuration. This definition will be used unless# you already have defined some toolsets in your user-config.jam# file.if ! mingw in [ feature.values <toolset> ]{    using mingw ; }project : default-build <toolset>mingw ;# List of --with-<library> and --without-<library># options. If left empty, all libraries will be built.# Options specified on the command line completely# override this variable.libraries =  ;# These settings are equivivalent to corresponding command-line# options.option.set prefix : /usr/local ;option.set exec-prefix : /usr/local ;option.set libdir : /usr/local/lib ;option.set includedir : /usr/local/include ;

?

?

修改后变成(修改三个地方)

?

# Boost.Build Configuration# Automatically generated by bootstrap.shimport option ;import feature ;# Compiler configuration. This definition will be used unless# you already have defined some toolsets in your user-config.jam# file.if ! gcc in [ feature.values <toolset> ]{    using gcc ; }project : default-build <toolset>gcc ;# List of --with-<library> and --without-<library># options. If left empty, all libraries will be built.# Options specified on the command line completely# override this variable.libraries =  ;# These settings are equivivalent to corresponding command-line# options.option.set prefix : /usr/local ;option.set exec-prefix : /usr/local ;option.set libdir : /usr/local/lib ;option.set includedir : /usr/local/include ;

?

?

?


7. 执行bjam编译
如果你已经编译过的话,可以把内容重定向到build.log
./bjam >build.log 2>&1
运行的时候也可以用EditPlus打开build.log查看。
如果第一次编译,建议直接在控制台上看输出(虽然没啥可看的)
./bjam
经过漫长的编译(大概十几分钟),编译完成,
打开boost目录下stage\lib,里面会有一些.a文件,一般分成两套mt,mt-d
估计是multiple threads和multiple threads debug的意思。

?


8. 安装boost
如果你想直接使用,可以以boost编译目录作为-I或-L目录,例如:
D:\software\mingwdev\msys-1.0.10\1.0\home\Administrator\boost_1_43_0
D:\software\mingwdev\msys-1.0.10\1.0\home\Administrator\boost_1_43_0\stage\lib
或者执行
./bjam install
它会等待一段时间,然后开始复制(因为它要检查所有的库),把boost的include和lib拷贝到当前驱动盘的usr目录下
例如D:\usr\local(注意不是在msys的目录下)

?

?

9. 重新拷贝lib和include目录
把D:\usr\local的内容拷贝会msys的目录下,一般是放在/local下,方便使用
(使用-I,-L,-l参数引用其中的.hpp和.a文件)

?

?


10. 查看build.log,看哪些文件编译失败。
如果你使用./bjam >build.log 2>&1编译,可以搜索"failed",看那些地方编译出错,
搜索文件名,还可以发现出错的行号和原因。
你也可以再次执行./bjam,也可以重新输出编译出错的信息

?


二、使用boost库的Header-Only Libraries在mingw中编译
由于boost很多库都不需要build
参考boost源码包中more\getting_started\unix-variants.html
官方给的例子

#include <boost/lambda/lambda.hpp>#include <iostream>#include <iterator>#include <algorithm>int main(){    using namespace boost::lambda;    typedef std::istream_iterator<int> in;    std::for_each(        in(std::cin), in(), std::cout << (_1 * 3) << " " );}

?

?

?

?


只要简单执行(假设boost目录在../boost_1_43_0下)
c++ -I../boost_1_43_0 example.cpp -o example.exe
即可编译。但这种方法只适用于Header-Only库(只有hpp文件的boost库)

?

?

三、VS2008下编译安装Boost
1. 安装VS2008。这个不多说了,因为我自己是用VC2008。
也可以考虑其他版本的VC,最好参考
http://www.boost.org/users/news/上历史版本的release notes,
那里会提供一个列表说明支持哪些版本的编译器,
例如Boost 1.41.0最低支持Visual C++ 7.1。
VC 6则不支持。

?


2. 安装CMake
http://www.cmake.org/cmake/resources/software.html
选择Windows (Win32 Installer)下载然后安装。
装完后确保加入path,可以在命令提示符号(cmd)下执行cmake -version查看版本。

?

3. 下载Boost-CMake
https://svn.boost.org/trac/boost/wiki/CMake
下载http://sodium.resophonic.com/boost-cmake/1.41.0.cmake0
解压到D:\software\mingwdev\boost\boost-1.41.0.cmake0\boost-1.41.0.cmake0

?

4. 执行CMake生成VC2008项目文件
cmd命令行下执行
cd D:\software\mingwdev\boost\boost-1.41.0.cmake0\boost-1.41.0.cmake0
D:
切换到Boost-CMake根目录
执行cmake --help可以查看-G参数
因为我是用VC9,所以执行
cmake -G "Visual Studio 9 2008"
目录下会多出几个工程文件.vcproj和Boost.sln

?

5. 编译
打开Boost.sln
里面或列出一些需要编译的库(除了那些Header-Only Libraries的库,还有一些依赖其他外部库的库)
可以按照需要编译感兴趣的库,例如boost_regex-mt-static
一般每个需要编译的库都包括一些后缀,例如mt是指多线程库(线程安全)。
对INSTALL右键->生成(注意先选择Debug还是Release)
可以全部编译所有库(需要比较长的时间)
如果有Warning可以无视,编译完成后会进行一堆的install

?

?

84>正在执行生成后事件... 
84>-- Install configuration: "Debug" 
84>-- Installing: C:/Program Files (x86)/Boost/include/boost 
84>-- Installing: C:/Program Files (x86)/Boost/include/boost/accumulators 
...

?

?

你可以去相应的目录查看
C:/Program Files (x86)/Boost
剪切出来,放到一个独立的地方保存。

?


6. 使用Boost
在工程属性中指定附加包含目录,和附加依赖项,附加库目录
或者在工具->选项->项目和解决方案->VC++目录中加入全局的include和lib

?

?


四、VS2008下编译使用Boost Header-Only Libraries
不需要编译,情况同“二”。
在工程属性中指定附加包含目录,
或者在工具->选项->项目和解决方案->VC++目录中加入全局的include

?

?


五、在MinGW中编译Boost.Python
1) 下载Python的Windows安装包并安装。我安装的是Python 2.7
http://www.python.org/ftp/python/2.7/python-2.7.msi
不推荐自己编译Python。

?

2) 修改project-config.jam
一般,由于MinGW不会自带python的MinGW编译版(除非你有能力编译出来=_=)。
所以在(一)中提到执行./bjam后最开始会出现这样的错误

?

WARNING: No python installation configured and autoconfiguration 
failed. See http://www.boost.org/libs/python/doc/building.html 
for configuration instructions or pass --without-python to 
suppress this message and silently skip all Boost.Python targets

?

执行./bootstrap.sh --help会提示:

?

?

--with-python=PYTHON specify the Python executable [python] 
--with-python-root=DIR specify the root of the Python installation 
[automatically detected] 
--with-python-version=X.Y specify the Python version as X.Y 
[automatically detected]

?

?

我试图使用--with-python=/c/Python27/python.exe,结果不成功。

后来仔细看完http://www.boost.org/libs/python/doc/building.html恍然大悟。
因为bjam是我在MinGW下编译的,而Python2.7也是VC编译的,导致gcc版的toolset不支持这样的project-config.jam设置:

# Python configuration 
using python : 2.7 : /c/Python27 ;

?
如果要正确编译Boost.Python,必须这样修改project-config.jam

?

?

# Boost.Build Configuration# Automatically generated by bootstrap.shimport option ;import feature ;# Compiler configuration. This definition will be used unless# you already have defined some toolsets in your user-config.jam# file.if ! gcc in [ feature.values <toolset> ]{    using gcc ; }project : default-build <toolset>gcc ;# Python configurationusing python     : 2.7 # version     : c:\\Python27\\python.exe # cmd-or-prefix     : c:\\Python27\\include # includes     : c:\\Python27\\libs # libraries     : # condition     ;# List of --with-<library> and --without-<library># options. If left empty, all libraries will be built.# Options specified on the command line completely# override this variable.libraries =  ;# These settings are equivivalent to corresponding command-line# options.option.set prefix : /usr/local ;option.set exec-prefix : /usr/local ;option.set libdir : /usr/local/lib ;option.set includedir : /usr/local/include ;

?

?

注意,我的Python是Python2.7,所以应该设置为

?

?

# Python configuration 
using python 
: 2.7 # version 
: c:\\Python27\\python.exe # cmd-or-prefix 
: c:\\Python27\\include # includes 
: c:\\Python27\\libs # libraries 
: # condition 
;

?

?

?

然后执行./bjam --with-python编译(如果前面已经全部编译Boost的其他库,也可以直接执行./bjam),就不会出现WARNING: No python installation configured的错误
关于这个手工配置,也可以参考官方的说明
http://www.boost.org/doc/libs/1_43_0/libs/python/doc/building.html#examples
不过他给的居然是Intel C++的例子,囧。

?

3) 执行./bjam --with-python install把编译的Boost.Python相关文件拷贝至D:\usr\local下(不是相对于msys的根目录)如果前面已经全部编译Boost的其他库,也可以直接执行./bjam install,不过个人觉得Boost.Python不是单独的库,这里最好别加--with-python。

?


4)测试Boost.Python的embedding
cd libs/python/example/quickstart
../../../../bjam --help
然后把bin.v2\libs\python\build\gcc-mingw-4.5.0\debug下的
libboost_python-mgw45-d-1_43.dll
复制到libs\python\example\quickstart\bin\gcc-mingw-4.5.0\debug下
(保证embedding.exe可以访问这个dll)
然后执行
./bin/gcc-mingw-4.5.0/debug/embedding.exe test_extending.py
会得到这样的结果

?

registering extension module embedded_hello... 
defining Python class derived from Base... 
testing derived class from C++... 
success! 
running file script.py... 
success! 
intentionally causing a python exception... 
Hello World ! 
No errors detected. 
Traceback (most recent call last): 
File "<string>", line 1, in <module> 
NameError: name 'unknown' is not defined

?

?

说明Boost.Python的embedding功能大致正常(虽然不知道哪里出了点错)

?

5)Boost.Python的extending
然后把libs\python\example\quickstart\bin\gcc-mingw-4.5.0\debug下的
extending.pyd和libboost_python-mgw45-d-1_43.dll(刚才从bin.v2\libs\python\build\gcc-mingw-4.5.0\debug拷贝过来的dll)
拷贝到libs\python\example\quickstart下。也就是说
libboost_python-mgw45-d-1_43.dll
test_extending.py
extending.pyd
这三个文件放在同一个文件夹内
如果使用Dependency Walker(一个dll导入表的检查程序)查看extending.pyd
会发现extending.pyd其实是引用了libboost_python-mgw45-d-1_43.dll的dll文件。
在msys命令行下执行
/c/Python27/python test_extending.py
输出结果是

?

写道Trying: 
from extending import * 
Expecting nothing 
ok 
Trying: 
hi = hello('California') 
Expecting nothing 
ok 
Trying: 
hi.greet() 
Expecting: 
'Hello from California' 
ok 
Trying: 
invite(hi) 
Expecting: 
'Hello from California! Please come soon!' 
ok 
Trying: 
hi.invite() 
Expecting: 
'Hello from California! Please come soon!' 
ok 
Trying: 
class wordy(hello): 
def greet(self): 
return hello.greet(self) + ', where the weather is fine' 
Expecting nothing 
ok 
Trying: 
hi2 = wordy('Florida') 
Expecting nothing 
ok 
Trying: 
hi2.greet() 
Expecting: 
'Hello from Florida, where the weather is fine' 
ok 
Trying: 
invite(hi2) 
Expecting: 
'Hello from Florida! Please come soon!' 
ok 
1 items had no tests: 
test_extending.run 
1 items passed all tests: 
9 tests in test_extending 
9 tests in 2 items. 
9 passed and 0 failed. 
Test passed.

?

?

说明Boost.Python的extending功能正常。

?

?


6)其实上Boost.Python还包括一个工具叫pyste
在libs\python\pyste下,关于它的使用,
由于我对Python不是太熟,所以就不多说了。

?

?

?

?

六、在VC2008中编译Boost.Python
1)按照(三)执行cmake生成.vcproj和.sln工程文件之后,
打开CMakeCache.txt(也是cmake生成的),搜索"python",
修改其键值,例如我改为指向Python的安装路径(假设是Python2.7)

?

//Path to a program.PYTHON_EXECUTABLE:FILEPATH=C:/Python27/python.exe//Path to a file.PYTHON_INCLUDE_DIR:PATH=C:/Python27/include//Path to a library.PYTHON_LIBRARY:FILEPATH=C:/Python27/libs/python27.lib

?

?

重新打开Boost.sln,出现python的工程,
再编译一次即可获得Boost.Python的lib文件。
(注意先选择Debug还是Release再执行生成)

?

?

?

2)测试
TODO:(待补充)

?

?

?

七、其他编译错误和警告的解决
TODO:(待补充)

?

?

?


(未完,待修改)


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值