標籤:
原創作品,允許轉載,轉載時請務必以超連結形式標明文章 原始出處 、作者資訊和本聲明。否則將追究法律責任。http://co63oc.blog.51cto.com/904636/504469
windows下用vs2008和boost結合編譯器
vc6.0和boost結合出現很多錯誤
使用asio子庫中一個http server的樣本程式,代碼在libs\asio\example\http\server目錄下。
1. 下載boost源檔案
http://sourceforge.net/projects/boost/files/boost/1.46.0/
2. 下載boost編譯管理工具bjam,它調用系統安裝的編譯器編譯來源程式
http://sourceforge.net/projects/boost/files/boost-jam/3.1.18/boost-jam-3.1.18-1-ntx86.zip
3. 假設boost的壓縮檔解壓到F:\boost,boost-jam解壓到F:\boost-jam
開啟命令提示字元,
F:
cd F:\boost
F:\boost-jam\bjam.exe install
這樣會編譯並安裝boost庫到C:\boost
4. vs2008中開啟菜單Tools->Options->Projects and Solutions->VC++ Directories
在Include files中增加C:\Boost\include
在Library files中增加C:\Boost\lib
5. 建立vs 2008 MFC Application項目mfc2008_boost1,
使用Release配置
Project->Project Properties...->Configuration Properties->C/C++->Precompiled Headers中設定Create/Use Precompiled Header為Not Using Precompiled Headers,即不使用先行編譯頭,否則編譯boost樣本程式出現先行編譯錯誤。
6. 複製boost源碼中libs\asio\example\http\server下所有hpp, cpp檔案到項目代碼目錄
7. 編輯mfc2008_boost1Dlg.cpp。
這是增加的代碼,從win_main.cpp中複製。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include "server.hpp"
#include "mfc2008_boost1.h"
#include "mfc2008_boost1Dlg.h"
Cmfc2008_boost1Dlg類中增加一線程函數:
unsigned int Cmfc2008_boost1Dlg::Thread1(LPVOID param)
要增加及修改代碼:
boost::function0<void> console_ctrl_function;
BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
{
switch (ctrl_type)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_SHUTDOWN_EVENT:
console_ctrl_function();
return TRUE;
default:
return FALSE;
}
}
unsigned int Cmfc2008_boost1Dlg::Thread1(LPVOID param)
{
try
{
// Initialise server.
http::server::server s("localhost", "88", "F:\\"); //本地88連接埠監聽,訪問根目錄為F:\
// Set console control handler to allow server to be stopped.
console_ctrl_function = boost::bind(&http::server::server::stop, &s);
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
// Run the server until stopped.
s.run();
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
}
return 0;
}
8. OnInitDIalog中增加線程建立代碼:
AfxBeginThread(Thread1, NULL);
9. 刪除win_main.cpp
10. 按F7編譯項目。Ctrl+F5運行
11. 瀏覽器開啟http://localhost:88/,出現404錯誤提示,這說明服務已經啟動。輸入 http://localhost:88/+"F:\ 下檔案名稱" 可訪問。
編譯boost需要很長時間,有網站製作了編譯好的boost庫,www.boostpro.com
下載安裝程式,按提示選擇編譯環境和需要的庫安裝。
windows下用vs2008和boost結合編譯器