windows下编译和配置boost

来源:互联网 发布:硬盘ntfs 可复制 mac 编辑:程序博客网 时间:2024/05/29 03:38

1 确保安装了visual studio ,我装了vs2013。

2 配置好visual studio 环境变量。

转自:http://www.cnblogs.com/bluestorm/p/3321558.html

方法1.运行脚本vsvars32.bat:
D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\vsvars32.bat

这个批处理 主要就是在运行CMD的时候先为我们设置一下环境变量(临时的) (这个脚本中写入的是bin, lib,include , tools的路径信息,也可以自己配置)


方法2.设置系统环境变量:
电脑右键属性-->高级环境变量-->系统环境变量选择PATH 编辑,将以下内容复制追加:
;D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE;D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools;D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin;D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcpackages;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Windows\Microsoft.NET\Framework64\v3.5;C:\Windows\Microsoft

环境变量生效可能需要重启计算机。

3 下载boost

官网地址:http://www.boost.org/ 下载最新的稳定版即可,我用的是1.59.0

打开VS2013 Native Tools Command。

可以从“开始”-->“所有程序”-->“Microsoft Visual Studio 2013”--> "Visual Studio Tools" --> "VS2013 x86 Native Tools Command Prompt"

5 解压boost

可以解压到任意目录,推荐某个盘符的根目录,我的是D:\Boost。

6 在“VS2013 x86 Native Tools Command Prompt”中进入到boost目录,执行bootstrap.bat

在我的环境中执行如下命令:

D:

cd D:\boost_1_59_0\boost_1_59_0

bootstrap.bat

7 编译

以下引自:http://jingyan.baidu.com/article/a3aad71aa1ebe7b1fb009681.html

        编译,可以简单的使用b2 install,也可以指定存放目录,或者寻找网上其它帮助文章。

        查看帮助可以输入:.\b2 --help

        比如要开启多线程编译:b2 install threading=multi

        设置生成的是debug或者release

        备注:如果是使用VS2013,请指定输出库类型,否则会缺一个lib文件:

        "无法打开文件 libboost_thread_vc120_mt_sgd-1_55.lib"。

        在2013时,我是使用下面的语句进行编译:

     

// 如果要获取动态库:

bjam install stage --toolset=msvc-12.0 --stagedir="C:\Boost\boost_vc_120" link=shared runtime-link=shared threading=multi debug release  

// 如果是要获取静态库:

bjam install stage --toolset=msvc-12.0 --stagedir="C:\Boost\boost_vc_120" link=static runtime-link=static threading=multi debug release  

    其中,注意修改--toolset=msvc-12.0,将12.0修改成对应的vs版本号,12.0是VS2013的版本号。

    目标地址也要修改成你所需的。

    注意,不要漏了install,它会帮你把头文件集合到一个文件夹中。

       编译过程有一个复制过程,编译需要的时间比较长,本次编译过程中,会在C盘根目录下生成一个boost文件夹,然后包含include和lib文件夹,这就是我们将要使用的头文件和库文件。

以下引自:http://blog.csdn.net/aalbertini/article/details/8612765

编译64位版本

1) 从开始菜单启动vs2010下的x64 win64 cmd窗口

2) 到BOOST下执行boostrap.bat生成相应版本的bjam

3) bjam.exe --toolset=msvc-10.0 architecture=
x86 address-model=64 link=static --build-type=complete --with-system --with-thre
ad --with-date_time --with-filesystem --with-serialization

8 配置使用环境

(1)在vs中打开属性管理器。

(2)右击debug或release,然后点击添加新属性表。

(3)在common properties --> c/c++ -->general --> additional include directories 中添加目录:D:\Boost\include\boost-1_59\boost

(4)在common properties --> Linker -->general --> additional library directories 中添加目录:D:\Boost\lib

以上两个加粗的目录可能需要根据你的具体情况来改变。

9 验证

以下代码引自:http://www.linuxidc.com/Linux/2014-06/103499.htm

#include "stdafx.h"
#include "boost/thread.hpp"
#include "iostream"
using namespace std;

void mythread()
{
 cout << " hello,thread! " << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
 boost::function<void()> f(mythread);
 boost::thread t(f);
 t.join();
 cout << " thread is over! " << endl;


 return 0;
}

如果以上代码可以运行,那么说明你的boost库已经成功编译并且配置成功了。

参考:1 http://jingyan.baidu.com/article/a3aad71aa1ebe7b1fb009681.html

           2 http://www.linuxidc.com/Linux/2014-06/103499.htm

                3 编译64位boost  参见:[1]http://blog.sina.com.cn/s/blog_52f26d0f0102v6hk.html

                  之前编译BGSLibrary时候使用的32位的boost,现在需要编译的Caffe是采用64位的boost库文件,所以要重新编译64位的boost

                  和32位环境不同,x64环境下编译得先从开始菜单启动Visual StudioVisual Studio Tools下的“VS2012 x64 兼容工具命令提示”进入命令提示符,而不是随便打开任                     意一个命令行窗口就行。然后转到boost根文件夹,运行bootstrap.bat生成x64版的bjam.exe。然后运行命令: 
                  bjam --build-type=complete threading=multi link=shared address-model=64 
                  即可生成lib文件,如果要编译静态库版就把shared改为static

·  要有address-model=64属性,如果没有这个属性的话,会默认生成32位的平台库,加入这个选项才能生成64位的DLL

·  link选项在我下载的版本中是不起所用的,只能编译成shared版本,原因有机会再探究吧。[1]

    


0 0
原创粉丝点击