asio-非同步編譯錯誤

來源:互聯網
上載者:User
#include <iostream>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/asio.hpp>

void print(const boost::system::error_code  &e,
           boost::asio::deadline_timer* t,  int *count)
{
    if ( *count < 5)
    {
        std::cout << *count << " ";
        ++(*count);

        t->expires_at( t->expires_at() + boost::posix_time::seconds(1) );
        t->async_wait( boost::bind(print,
            boost::asio::placeholders::error, t, count ));
    }
}

int main()
{
    boost::asio::io_service io;

    int count = 0;

    boost::asio::deadline_timer t ( io, boost::posix_time::seconds(1) );

    t.async_wait( boost::bind(print,
        boost::asio::placeholders::error , &t, &count ));

    io.run();

    std::cout << "final count is :" << count << std::endl;

    return 0;

}

將上面的內容(asio官方的非同步timer教程)在vs2005下編譯時間會出現一個錯誤,該錯誤是一個包含windows.h標頭檔的錯誤,雖然我們沒有編寫包含windows.h標頭檔的申明,但是asio庫中的microsec_time_clock.hpp標頭檔包含了windows.h標頭檔,因此修改此方案的方式在包含windows.h標頭檔的前面寫一個宏定義:

#ifdef BOOST_HAS_FTIME
#define WIN32_LEAN_AND_MEAN             // 這一行是後來添加進去的
#include <windows.h>
#endif

此時,再次編譯,有可能會出現無法找到libboost_date_time-vc80-mt-s-1_35.lib庫檔案,修改的方案是將libboost_date_time-vc80-mt-1_35.lib庫檔案複製一份,然後命名為libboost_date_time-vc80-mt-s-1_35.lib,至於為什麼這樣做是可以的,我也不是很清楚。

再次編譯,可能還會出現找不到libboost_system-vc80-mt-s-1_35.lib庫檔案的錯誤,同上面的,將libboost_system-vc80-mt-1_35.lib庫檔案更名為libboost_system-vc80-mt-s-1_35.lib。

說明:
  1、vc80 -- 即表示vs2005
  2、如果你沒有上面的庫,那麼google一下,如何編譯boost庫,google上面很多很多。既然要用boost,免不了需要編譯部分boost庫,如何編譯僅需要的庫我還沒有學會,因此不要問我啦!
  3、希望該記錄能夠為那些碰到類似問題的網友提供一個拋磚引玉的方法。
 
參考:
 關於windows.h標頭檔錯誤的提問:
      http://www.nabble.com/WinSock.h-has-already-been-included-td18329342.html

後記:
  後來還給出了另外一種實現方式,但是編譯時間發現較多錯誤,因此也在這邊記錄下來。

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

class  printer
{
public:
    printer(boost::asio::io_service &io)
        : _timer(io, boost::posix_time::seconds(1))
        , _count(0)
    {
        _timer.async_wait( boost::bind<void>( boost::mem_fn(&printer::print),this));
       // _timer.async_wait( boost::bind( &printer::print,this) ); //編譯錯誤,修改為上面方式即可
    }

    ~printer()
    {
        std::cout << "Final count is " << _count << " " << std::endl;
    }
    void print()
    {
        if( _count < 5)
        {
            std::cout << _count << std::endl;
            _count++;
            _timer.expires_at( _timer.expires_at() + boost::posix_time::seconds(1) );
            _timer.async_wait( boost::bind<void>(boost::mem_fn(&printer::print), this));
      // _timer.async_wait( boost::bind( &printer::print,this) ); //編譯錯誤,修改為上面方式即可

        }
    }
protected:
private:
    boost::asio::deadline_timer _timer;
    int    _count;
};

int main()
{
    //boost::asio:io_service io;
    boost::asio::io_service io;
    printer p(io);
    io.run();

    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.