vs2010使用boost库,安装

来源:互联网 发布:libreoffice软件 编辑:程序博客网 时间:2024/06/04 19:04

今天抽时间学习了一下boost库,用c++ 做算法,自己再去造轮子实在是浪费时间,学习boost未来工作能直接上手。

比如caffe就直接使用了boost库, 这里边常用的 对于时间操作,字符串操作,文件操作,智能指针等,熟练使用这些能大大的加速算法的开发时间。


一、参考资料:

   1、 http://download.csdn.NET/detail/nuoshueihe/5344610    boost程序库完全开发指南–深入C++标准库    书,写的还不错,但是不如看英文文档更直接

2、http://www.boost.org/  boost 官网,在这里下载Version 1.60.0,以及其文档

3、http://blog.csdn.Net/kangroger/article/details/39393769  参考编译文档


二、编译过程:

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

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

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

用VS2010命令行

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


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

(3)用bjam.exe编译

运行命令bjam stage  –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 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

 –without-Python 我电脑上提示没有Python,所以把这个库去掉。

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。



 

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

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #include “stdafx.h”  
  2. #include “boost/thread.hpp”    
  3. #include <iostream>  
  4.   
  5. using namespace std;  
  6. void thread_func()  
  7. {  
  8.     cout << ”This is my first boost,thread function test” << endl;  
  9. }  
  10. int _tmain(int argc, _TCHAR* argv[])  
  11. {  
  12.     boost::function<void()> func(thread_func);  
  13.     boost::thread t(func);  
  14.     t.join();  
  15.     system(”puase”);  
  16.     return 0;  
  17. }  
#include "stdafx.h"

#include "boost/thread.hpp" #include <iostream>using namespace std;void thread_func(){ cout << "This is my first boost,thread function test" << endl;}int _tmain(int argc, _TCHAR* argv[]){ boost::function<void()> func(thread_func); boost::thread t(func); t.join(); system("puase"); return 0;}


测试 可以运行,安装成功

0 0
原创粉丝点击