boost:asio编译

来源:互联网 发布:linux gcc 安装 编辑:程序博客网 时间:2024/05/15 21:14

转载自:http://www.cnblogs.com/lidabo/p/3782293.html

环境: VS2012,

          boost_1_61_0,解压缩后放在,D:/boost_1_61_0。

1,编译。

   boost库大部分源文件是只有投文件,所以有很多库不用编译就可以使用。但是有些库是需要编译源码的。asio就需要编译。

   怎么去编译呢?打开vs2012 Visual Studio Tools下面的VS2012 x64 兼容工具命令提示,运行bootstrap.bat gcc编译出bjam.exe。 因为asio依赖于其它的一些库,所以编译参数还有点复杂。然后在cmd下输入:

D:/boost_1_61_0>bjam --with-system --with-thread --with-date_time --with-regex --with-serialization stage

   

   编译完成后就可以在boost_1_61_0/stage里面找到编译好的库文件。

   有时候你的系统上面可能装了几个版本的VS,那么怎么指定版本呢?

D:/boost_1_38_0>bjam --without-python --toolset=msvc-11.0 --with-thread --with-date_time --with-regex --with-serialization stage

 

--without-python 表示不使用 python
--toolset : 所使用compiler,Visual Studio 2012為msvc-11.0
--prefix:指定編譯後library的安裝目錄

 

接下来就是导入include目录boost根目录到vs中,导入编译后的lib文件目录stage/lib到lib路径中去。

vs2012:右击project->properties->VC++ Directories. 将D:/boost_1_61_0加入到include directories中去,将D:/boost_1_61_0/stage/lib加入到Library Directories路径中去。

 

2, 尝试第一个程序。

把asio下面的文档中的第一个例子抄下来.

//  // timer.cpp  // ~~~~~~~~~  //  // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)  //  // Distributed under the Boost Software License, Version 1.0. (See accompanying  // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)  //    #include <iostream>  #include <boost/asio.hpp>  #include <boost/date_time/posix_time/posix_time.hpp>    int main()  {    boost::asio::io_service io;      boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));    t.wait();      std::cout << "Hello, world!/n";      return 0;  }  

编译,成功!


如果编译报错请看下面:

1>LINK : fatal error LNK1104: cannot open file 'libboost_system-vc100-mt-gd-1_38.lib'

仔细一查,确实没有找到这个文件,怎么办?

可能是没有编译debug文件,暂时也不知道编译的时候该添加哪个参数让它编译debug文件。于是将编译选项output改为release,再编译。

编译出错。

LINK : fatal error LNK1104: cannot open file 'libboost_regex-vc100-mt-1_38.lib'

 

 网上google了一把,看到下面文字

注意:

使用MSVC或Borland C++,你可能需要在“工程设置”中分别添加 -DBOOST_DATE_TIME_NO_LIB 和-DBOOST_REGEX_NO_LIB 声明,分别禁止Boost.Date_Time和Boost.Regex的自动链接,当然你也可以这样做:build这两个库,然后链接。


试试。加 -DBOOST_DATE_TIME_NO_LIB 和-DBOOST_REGEX_NO_LIB 声明到工程选项的c/C++/commandline后面,编译,成功!高兴!

运行,ok!

bjam参数--build-dir=<builddir>编译的临时文件会放在builddir里(这样比较好管理,编译完就可以把它删除了)--stagedir=<stagedir>存放编译后库文件的路径,默认是stage--build-type=complete编译所有版本,不然只会编译一小部分版本(确切地说是相当于:variant=release, threading=multi;link=shared|static;runtime-link=shared)variant=debug|release决定编译什么版本(Debug or Release?)link=static|shared决定使用静态库还是动态库。threading=single|multi决定使用单线程还是多线程库。runtime-link=static|shared决定是静态还是动态链接C/C++标准库。--with-<library>只编译指定的库,如输入--with-regex就只编译regex库了。--show-libraries显示需要编译的库名称
0 0
原创粉丝点击