时遇到编译在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