參考:http://hi.baidu.com/need_for_dream/blog/item/c14a28086a504c33e92488b5.html
環境: VS2010,
boost1.38.0,解壓縮后放在,D:/boost_1_38_0。
編譯bjam(這個我沒有試過,轉過來以后參考)
利用Visual Studio 2005 Command Prompt開啟DOS視窗,將目錄cd到C:/boost_1_34_1/tools/jam/src下,執行build.bat,然後會在C:/ boost_1_38_0/tools/jam/src/bin.ntx86/產生bjam.exe,將bjam.exe複製到c:/ boost_1_38_0/下
1,編譯。
boost庫大部分源文件是只有投文件,所以有很多庫不用編譯就可以使用。但是有些庫是需要編譯源碼的。asio就需要編譯。
怎么去編譯呢?在boost官方網站下載bjam.exe,放入boost源文件的根目錄下面。因為asio依賴於其它的一些庫,所以編譯參數還有點復雜。然后在cmd下輸入
D:/boost_1_38_0>bjam --with-system --with-thread --with-date_time --with-regex -
-with-serialization stage
編譯完成后就可以在boost_1_38_0/stage里面找到編譯好的庫文件。如果在編譯的時候出現編譯器方面的錯誤,可以嘗試運行C:/Program Files/Microsoft Visual Studio 9.0/VC/vcvarsall.bat,自動設置編譯環境。
有時候你的系統上面可能裝了幾個版本的VS,那么怎么指定版本呢?
D:/boost_1_38_0>bjam --without-python --toolset=msvc-10.0 --with-thread --with-date_time --with-regex -
-with-serialization stage
--without-python 表示不使用 python
--toolset : 所使用compiler,Visual Studio 2010為msvc-10.0
--prefix:指定編譯後library的安裝目錄
接下來就是導入include目錄boost根目錄到vs中,導入編譯后的lib文件目錄stage/lib到lib路徑中去。
vs2010:右擊project->properties->VC++ Directories. 將D:/boost_1_38_0加入到include directories中去,將D:/boost_1_38_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編譯參數請參考http://www.cppprog.com/2009/0112/48.html;轉載如下:
--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 | 顯示需要編譯的庫名稱 |