使用Boost ASIO的Windows 7 MinGW编译错误

时间:2014-03-29 10:26:55

标签: compilation mingw boost-asio

在Windows 7上编译以下C ++代码时遇到问题:

#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();
}

我已在c:\mingw中安装了MinGW(gcc 4.8.1)并正确设置了PATH。我已经下载了boost和声明的环境变量BOOST_ROOT作为它所在的路径。我已通过bootstrapb2程序进行提升。我现在尝试编译:

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_ROOT%/stage/lib中搜索了boost_system并找到了静态库(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]

我哪里错了?

1 个答案:

答案 0 :(得分:1)

我继续用b2 toolset=gcc --build-type=complete再次重建了Boost。同样的事发生了。最后,事实证明,我所需要的只是将链接放在命令的末尾:

C:\path\to\sandbox> g++ -D_WIN32_WINNT=0x0601 -I%BOOST_ROOT% -L%BOOST_ROOT%\stage\lib -o boosttest boosttest.cpp -lwsock32 -lws2_32 -lboost_system-mgw48-mt-d-1_55

C:\path\to\sandbox> boosttest.exe
5 s.
10 s.

-D_WIN32_WINNT仍然是必要的,对于任何跳过其他评论的人,我必须修补winsock.h详细http://sourceforge.net/p/mingw/bugs/1980/。并且记得将%BOOST_ROOT%\stage\lib放在PATH中,以便Windows可以在运行时找到dll。

艰巨