问题描述
在Windows 7上编译以下C ++代码时遇到问题:
#include< boost / asio.hpp>
#include< iostream>
void handler1(const boost :: system :: error_code& ec)
{
std :: cout< 5秒。 << std :: endl;
}
void handler2(const boost :: system :: error_code& ec)
{
std :: cout< 10秒。 << 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 与我的 PATH
正确设置。我已经下载boost并声明环境变量 BOOST_ROOT
是它所在的路径。我已经经历了升级的 bootstrap
和 b2
过程。我现在尝试编译:
c:\path\to\sandbox> g ++ -I%BOOST_ROOT%-o main main.cpp
提供一堆 错误:':: UnregisterWaitEx'尚未声明
错误
然后我搜索一下,可能需要链接 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\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
^
c:\mingw\include\mswsock.h:125:36:错误:期望的主表达式','token
int WSAAPI WSAPoll(WSAPOLLFD,ULONG,INT );
^
c:\mingw\include\mswsock.h:125:41:错误:预期的主表达式')'token
int WSAAPI WSAPoll(WSAPOLLFD,ULONG,INT );
^
c:\mingw\include\mswsock.h:125:41:错误:在初始化器中作为复合表达式处理的表达式列表[-fpermissive]
我会在哪里出错?
解决方案我继续使用
b2 toolet = 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
详细。请记住在PATH
中放置%BOOST_ROOT%\stage\lib
,以便Windows可以在运行时找到dll 。
艰难
Having trouble compiling the following C++ code on Windows 7:
#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(); }
I have MinGW installed (gcc 4.8.1) in
c:\mingw
with myPATH
set up correctly. I have downloaded boost and declared environment variableBOOST_ROOT
to be the path where it resides. I have gone through thebootstrap
andb2
procedure for boost. I now try and compile:c:\path\to\sandbox> g++ -I%BOOST_ROOT% -o main main.cpp
Gives a bunch of
error: '::UnregisterWaitEx' has not been declared
errorsI then search a bit and see I may need to link
boost_system
. So:c:\path\to\sandbox> g++ -I%BOOST_ROOT% -lboost_system -o main main.cpp
Same errors. Thought I'd try specify library path. Did a search for boost_system and found static libs (libboost_system-mgw48-mt-1_55.a) in
%BOOST_ROOT%/stage/lib
. Soc:\path\to\sandbox> g++ -I%BOOST_ROOT% -L%BOOST_ROOT%/stage/lib -lboost_system-mgw48-mt-1_55 -o main main.cpp
Same errors. So I search again and see others suggesting appending a
-D-D_WIN32_WINNT=0x0601
. Soc:\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
And the inevitable errors:
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]
Where am I going wrong?
解决方案I went ahead and rebuilt Boost again with
b2 toolset=gcc --build-type=complete
. Same thing happened. Finally, after all that, it turned out all I needed was to put the linking at the end of the command: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.
The
-D_WIN32_WINNT
was still necessary and, for anyone who has skipped the other comments, I had to patchwinsock.h
as detailed http://sourceforge.net/p/mingw/bugs/1980/. And remember to put%BOOST_ROOT%\stage\lib
in yourPATH
so Windows can find the dll at runtime.Arduous
这篇关于Windows 7使用Boost ASIO的MinGW编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!