VS2010编译Boost 1.56

时间:2023-03-09 08:39:09
VS2010编译Boost 1.56

(1)首先下载源代码:http://softlayer-dal.dl.sourceforge.net/project/boost/boost/1.56.0/boost_1_56_0.zip

解压到某个目录,我解压到了D盘根目录:D:\boost_1_56_0

(2)生成bjam.exe可执行文件

  用VS2010命令行:

VS2010编译Boost 1.56

进入到到目录D:\boost_1_56_0,运行booststrap.bat得到:

VS2010编译Boost 1.56

  这时在目录D:\boost_1_56_0生成了b2.exe、bjam.exe、project-config.jam文件。

(3)用bjam.exe编译

  运行命令bjam stage --without-python --toolset=msvc-10.0 --build-type=complete --stagedir="D:\boost_1_56_0\bin\vc10"  link=static runtime-link=shared threading=multi debug release

  bjam可以参考http://blog.chinaunix.net/uid-22301538-id-3158997.html

stage表示只生成库(dll和lib),用install的话还会生成包含头文件的include目录。
toolset指定编译器,VS2010用msvc-10.0。
without/with表示不编译/编译哪些库。
stagedir,当使用stage时用stagedir,使用install用prefix,表示编译生成文件的路径。路径的命名最好和编译器相关,编译管理。
link指定生成动态链接库或静态链接库。生成动态链接库需使用shared方式,生成静态链接库需使用static方式。
runtime-link,动态/静态链接C/C++运行时库。有shared和static两种方式,这样runtime-link和link一共可以产生4种组合方式。
threading,单/多线程编译。
debug/release,编译debug/release版本。一般都是程序的debug版本对应库的debug版本,所以两个都编译。

差不多需要一小时,编译完成(中间会有警告)。

编译好后,在根目录会有个bin.v2文件夹,是编译过程中的临时文件夹,很大,可以手动删除。

(4)在VS中使用Boost库

新建工程后需要把Boost库包含到工程中,右键选择属性,在VC++目录的“包含目录”中添加Boost的根目录,在“库目录”添加刚刚编译生成的位置再加上路径lib。

VS2010编译Boost 1.56

之后包好头文件即可使用,例如一个多线程的测试:

    1. #include "boost/thread.hpp"
    2. #include "iostream"
    3. using namespace std;
    4. void threadFunc()
    5. {
    6. cout << "This is a thread function" << endl;
    7. }
    8. int main()
    9. {
    10. boost::function<void()> func(threadFunc);
    11. boost::thread t(func);
    12. t.join();
    13. return 0;
    14. }