天天看点

Windows7 Code::Blocks & Boost 安装编译

第一步:下载Code::Blocks,访问Boost官网下载带gcc和g++编译器版本的

codeblocks-16.01mingw-setup.exe,下载完以后安装

第二步:下载Boost源码,访问地址(https://dl.bintray.com/boostorg/release/1.64.0/source/)选择boost_1_64_0.zip版本, 然后解压。我解压到“D:\dev_tool\boost_1_64_0”

第三步:打开CMD终端,然后cd到“D:\dev_tool\boost_1_64_0”,然后在该目录下有一个bootstrap.bat,通过执行bootstrap.bat gcc,然后生成bjam.exe和b2.exe,其中的gcc指定是编译器。

第四部:执行下面命令:

D:\dev_tool\boost_1_64_0>bjam install –toolset=gcc –without-graph –without-graph_parallel –without-mpi –without-serialization –without-wave –prefix=”D:\Boost\boost_1_64_0” link=static runtime-link=shared runtime-link=static threading=multi debug release 下面解释具体参数:

1.install/stage: install会生成包含头文件的include目录,stage表示只生成库(dll和lib)无Include目录(可以使用原本就有的booost_1_64_0目录下的boost目录)。我这里面用install命令,生成的include只有几十M不会占用太大空间。

2.toolset编译器选项可以是gcc、msvc、borland

3.without表示不安装某些库(因为我在学习Python所以我这里安装了python库,安装Python库需要首先安装Python解释器,您如果不需要的话可以添加–with-python来取消安装python库)

4.prefix/stagedir 前面使用install时用prefix,用stage时用stagedir,表示编译生成文件的路径,我这里是在D:\dev_tool\Boost下的Boost目录,您可以根据自己的偏好更好,同样配置C::B和VS时的也要相应地更改。

5.build-dir 编译过程中间文件存放目录,默认是bjam目录下的bin.v2,等编译完成后便可删掉该文件.

6.threading 一般是多线程所以multi(如果您不写多线程程序的话这里可以不用设置)

link 生成动态链接库/静态链接库。生成动态链接库需使用shared方式,生成静态链接库需使用static方式。一般boost库可能都是以static方式编译,因为最终发布程序带着boost的dll感觉会比较累赘。

7.runtime-link 动态/静态链接C/C++运行时库。同样有shared和static两种方式,这样runtime-link和link一共可以产生4种组合方式,各人可以根据自己的需要选择编译。一般link只选static的话,只需要编译2种组合即可,即link=static runtime-link=shared和link=static runtime-link=static,本人一般就编这两种组合。

8.debug/release 编译debug/release版本。一般都是程序的debug版本对应库的debug版本,所以两个都编译。

第五步:执行上面的指令后会在D:\dev_tool\Boost\booost_1_64_0下面生成两个文件夹:include和lib,然后设置 Code::Blocks中的全局变量boost。

Windows7 Code::Blocks & Boost 安装编译

具体步骤如下:

Setting->Global Variables

a. 单击New按钮,创建boost全局变量

b. 设置 base: D:\Boost\boost_1_64_0

c.设置include:D:\Boost\boost_1_64_0\include\boost-1_64\

d.设置lib: D:\Boost\boost_1_64_0\lib

第六步:设置编译器信息

1.设置支持C++11,勾选下图选项

Windows7 Code::Blocks & Boost 安装编译

2.找到Search directories选项,分别设置complier和linker项目的目录,点击add按钮在complier下输入(&#boost.include),在Linker下输入(&#boost.lib)入下图:

Windows7 Code::Blocks & Boost 安装编译

第七步:编写一个多线程测试案例

#include<thread>
 #include<iostream>
 #include<vector>

 using namespace std;

 void thread_task(){
    cout<<"当前任务的线程ID为:"<<this_thread::get_id()<<endl;
 }
 int main(){
    vector<thread> threads;
    for(int i=;i<;++i){
        threads.push_back(thread(thread_task));
    }
    for(auto &th : threads){
        th.join();
    }
    return ;
 }
           

总结:千呼万唤始出来,技术生涯哀哉,哀哉!最后恭喜大家迈出学习boost库的第一步,路漫漫其修远兮,吾将上下而求索。

继续阅读