0

I'm trying to work with boost

My includes look like this:

#include <boost/asio.hpp>
#include <Windows.h>

I'm trying to test out this call:

ShellExecute(NULL, "open", "C:/local/test.txt", NULL, NULL, SW_SHOWNORMAL);

When I compile I get "ShellExecute is undefined". If I move the Windows.h include above the boost include, it's picked up by the compiler but I get tons of Winsock errors instead. I'm using VS 2015.

This only happens with the network libraries - I've been using boost filesystem prior to this with no problems.

This happens with all functions that are provided by Windows.h

Any thoughts would be appreciated!

matt
  • 723
  • 1
  • 12
  • 20
  • 1
    You are doing battle with Boost using WIN32_LEAN_AND_MEAN in its private .h files. Very naughty. Best way is to `#include ` yourself first so Boost doesn't try it. – Hans Passant Jul 05 '16 at 12:09

1 Answers1

3

You need to #define WIN32_LEAN_AND_MEAN prior to including Windows.h, to prevent it from dragging in the Windows Sockets 1 headers (see Using the Windows Headers). Those headers collide with the Windows Sockets 2 headers used by boost asio.

The following fixes the issues with colliding Windows Sockets declarations:

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <boost/asio.hpp>

Note: Keep in mind, that defining the WIN32_LEAN_AND_MEAN preprocessor symbol skips inclusion of a number of system headers. To account for that, you need to explicitly include them. In case of ShellExecute this would be #include <Shellapi.h>.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • Hmm thanks for pointing me in the right direction but this doesn't fix it - just using `WIN32_LEAN_AND_MEAN` with no headers besides Windows.h causes `ShellExecute` to be undefined. Same problem as the OP when the Boost libraries are included. – matt Jul 05 '16 at 10:53
  • 1
    @matt: You need to `#include ` to use [ShellExecute](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153.aspx). The documentation tells you, which header to include (and which libraries to link against). – IInspectable Jul 05 '16 at 10:56
  • Thanks for your help, I managed to get it working, I was fumbling around a lot. :) – matt Jul 05 '16 at 11:52