在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
作为它所在的路径。我已通过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_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]
我哪里错了?
答案 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。
艰巨