2014-03-29 93 views
1

时遇到编译在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:\mingwPATH设置正确。我已经下载了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_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] 

我要去哪里错了?

+1

您需要链接到Winsock的窗户,加:-lwsock32 -lws2_32删除“不可避免的错误” – kenba

+0

谢谢,但仍然得到错误。 '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

+0

哦,我应该提到我必须修补'winsock2.h'这里指定 - http://sourceforge.net/p/mingw/bugs/1980/。否则我仍然会收到那些'UnregisterWaitEx'错误。 – artvandelay

回答

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/。并且请记得在您的PATH中放置%BOOST_ROOT%\stage\lib,以便Windows可以在运行时找到dll。

艰苦

相关问题