時遇到編譯在Windows 7下面的C++代碼:Windows 7的MinGW的編譯錯誤使用Boost ASIO
#include <boost/asio.hpp>
#include <iostream>
void handler1(const boost::system::error_code &ec)
{
std::cout << "5 s." << std::endl;
}
void handler2(const boost::system::error_code &ec)
{
std::cout << "10 s." << std::endl;
}
int main()
{
boost::asio::io_service io_service;
boost::asio::deadline_timer timer1(io_service, boost::posix_time::seconds(5));
timer1.async_wait(handler1);
boost::asio::deadline_timer timer2(io_service, boost::posix_time::seconds(10));
timer2.async_wait(handler2);
io_service.run();
}
我MinGW的安裝(GCC 4.8.1)在c:\mingw
我PATH
設置正確。我已經下載了boost並聲明瞭環境變量BOOST_ROOT
是它所在的路徑。我已經通過bootstrap
和b2
升壓程序。我現在嘗試編譯:
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -o main main.cpp
給出一堆error: '::UnregisterWaitEx' has not been declared
錯誤
然後我搜索了一下,看到我可能需要鏈接boost_system
。所以:
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -lboost_system -o main main.cpp
相同的錯誤。以爲我會嘗試指定庫路徑。搜索了boost_system並在%BOOST_ROOT%/stage/lib
中找到了靜態庫(libboost_system-mgw48-mt-1_55.a)。所以
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -L%BOOST_ROOT%/stage/lib -lboost_system-mgw48-mt-1_55 -o main main.cpp
相同的錯誤。所以我再次搜索並看到其他人建議追加一個-D-D_WIN32_WINNT=0x0601
。所以
c:\path\to\sandbox> g++ -I%BOOST_ROOT% -L%BOOST_ROOT%/stage/lib -lboost_system-mgw48-mt-1_55 -o main main.cpp -D_WIN32_WINNT=0x0601
而不可避免的錯誤:
c:\mingw\include\mswsock.h:125:20: error: 'WSAPOLLFD' was not declared in this scope
int WSAAPI WSAPoll(WSAPOLLFD, ULONG, INT);
^
c:\mingw\include\mswsock.h:125:36: error: expected primary-expression before ',' token
int WSAAPI WSAPoll(WSAPOLLFD, ULONG, INT);
^
c:\mingw\include\mswsock.h:125:41: error: expected primary-expression before ')' token
int WSAAPI WSAPoll(WSAPOLLFD, ULONG, INT);
^
c:\mingw\include\mswsock.h:125:41: error: expression list treated as compound expression in initializer [-fpermissive]
我要去哪裏錯了?
您需要鏈接到Winsock的窗戶,加:-lwsock32 -lws2_32刪除「不可避免的錯誤」 – kenba
謝謝,但仍然得到錯誤。 'g ++ -I%BOOST_ROOT%-L%BOOST_ROOT%/ stage/lib -lboost_system -mgw48 -mt-1_55 -lwsock32 -lws2_32 -o main main.cpp -D_WIN32_WINNT = 0x0601'給出了一大堆'未定義的'boost: :system :: generic_category()''錯誤。建議鏈接器在使用'boost_system'時遇到問題,但我真的不知道是什麼。 – artvandelay
哦,我應該提到我必須修補'winsock2.h'這裏指定 - http://sourceforge.net/p/mingw/bugs/1980/。否則我仍然會收到那些'UnregisterWaitEx'錯誤。 – artvandelay