Compilation 使用Boost ASIO时出现Windows 7 MinGW编译错误 在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(); }

Compilation 使用Boost ASIO时出现Windows 7 MinGW编译错误 在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(); },compilation,mingw,boost-asio,Compilation,Mingw,Boost Asio,给出了一组错误:'::UnregisterWaitEx'尚未声明错误 然后我搜索了一下,发现我可能需要链接到boost\u系统。因此: c:\path\to\sandbox> g++ -I%BOOST_ROOT% -lboost_system -o main main.cpp 同样的错误。我想我应该尝试指定库路径。搜索boost_system并在%boost_ROOT%/stage/lib中找到静态lib(libboost_system-mgw48-mt-1_55.a)。所以 c:\pa

给出了一组
错误:'::UnregisterWaitEx'尚未声明
错误

然后我搜索了一下,发现我可能需要链接到boost\u系统。因此:

c:\path\to\sandbox> g++ -I%BOOST_ROOT% -lboost_system -o main main.cpp
同样的错误。我想我应该尝试指定库路径。搜索boost_system并在
%boost_ROOT%/stage/lib
中找到静态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\u WIN32\u 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]

哪里出了问题?

我继续使用
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
。请记住将
%BOOST\u ROOT%\stage\lib
放在
路径中,以便Windows可以在运行时找到dll


艰巨

您需要链接到winsock for windows,添加:-lwsock32-lws2_32以删除“不可避免的错误”,谢谢,但仍然会出现错误
g++-I%BOOST\u ROOT%-L%BOOST\u ROOT%/stage/lib-lboost\u system-mgw48-mt-1\u 55-lwsock32-lws2\u 32-o main.cpp-D_WIN32\u WINNT=0x0601
给出了一系列
未定义的参考
BOOST::system::generic\u category()`错误。提示链接器在
boost\u系统中遇到问题,但我真的不知道是什么问题。哦,我应该提到我必须按照此处指定的方式修补
winsock2.h
。否则,我仍然会收到那些
UnregisterWaitEx
错误。仅供参考,我刚刚在我的系统上构建并运行了您的代码;它很好用。然而,我的链接顺序是:
-L%BOOST\u ROOT%/stage/lib-lwsock32-lws2\u 32-lboost\u system-mgw48-mt-d
…谢谢,但仍然没有乐趣。我使用图形化工具重新安装了mingw,而不是手动解包,我通过
bootstrap.bat mingw
然后
b2 toolset=gcc
重建了boost 1.55。然后我再次尝试:
g++-I%BOOST\u ROOT%-L%BOOST\u ROOT%\stage\lib-lwsock32-lws2\u 32-lboost\u system-mgw48-mt-1\u 55-o boosttest boosttest.cpp-D\u WINNT=0x0601
,按照您建议的顺序。仍然看到对boost::system::generic_category()的未定义引用。
错误。嗯,我不知道,我会再进行一次搜索,看看是否有其他人有类似的问题。
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.