1

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 my PATH set up correctly. I have downloaded boost and declared environment variable BOOST_ROOT to be the path where it resides. I have gone through the bootstrap and b2 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 errors

I 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. So

c:\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. So

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

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?

  • 2
    You need to link to winsock for windows, add: -lwsock32 -lws2_32 to remove the "inevitable errors" – kenba Mar 29 '14 at 11:02
  • Thanks but still getting errors. `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` gives a bunch of `undefined reference to `boost::system::generic_category()'` errors. Suggests linker is having trouble with `boost_system` but I don't really know what. –  Mar 29 '14 at 15:18
  • Oh, and I should mention I had to patch `winsock2.h` as specified here - http://sourceforge.net/p/mingw/bugs/1980/. Otherwise I was still getting those `UnregisterWaitEx` errors. –  Mar 29 '14 at 15:21
  • FYI I've just built and run your code on my system; it works fine. However, my link order is: `-L%BOOST_ROOT%/stage/lib -lwsock32 -lws2_32 -lboost_system-mgw48-mt-d`... – kenba Mar 30 '14 at 07:00
  • Thanks but still no joy. I reinstalled mingw using the graphical tool as opposed to unpacking the packages manually, and I've rebuilt boost 1.55 doing `bootstrap.bat mingw` then `b2 toolset=gcc`. Then I tried again: `g++ -I%BOOST_ROOT% -L%BOOST_ROOT%\stage\lib -lwsock32 -lws2_32 -lboost_system-mgw48-mt-1_55 -o boosttest boosttest.cpp -D_WIN32_WINNT=0x0601` in the order you suggested. Still seeing `undefined reference to boost::system::generic_category()` error. Hmm I don't know, I'll have another search and see if anyone else if having similar issues. –  Mar 30 '14 at 15:01

1 Answers1

1

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 patch winsock.h as detailed http://sourceforge.net/p/mingw/bugs/1980/. And remember to put %BOOST_ROOT%\stage\lib in your PATH so Windows can find the dll at runtime.

Arduous