包括 Boost 网络库使得 Windows.h 函数未定义

Including Boost network libraries makes Windows.h functions undefined

我正在尝试使用 boost

我的包含如下所示:

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

我正在尝试测试这个调用:

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

编译时得到 "ShellExecute is undefined"。如果我将 Windows.h include 移动到 boost include 之上,它会被编译器拾取,但我会得到大量的 Winsock 错误。我正在使用 VS 2015。

这只发生在网络库中 - 在此之前我一直在使用 boost 文件系统,没有任何问题。

Windows.h

提供的所有函数都会出现这种情况

如有任何想法,我们将不胜感激!

您需要 #define WIN32_LEAN_AND_MEAN 在包含 Windows.h 之前,以防止它拖入 Windows 插槽 1 headers(参见 Using the Windows Headers)。那些 headers 与 boost asio 使用的 Windows Sockets 2 headers 发生冲突。

以下修复了 Windows 套接字声明冲突的问题:

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

注意:请记住,定义 WIN32_LEAN_AND_MEAN 预处理器符号会跳过一些系统 headers 的包含。为了解决这个问题,您需要明确地包括它们。在 ShellExecute 的情况下,这将是 #include <Shellapi.h>.