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

Including Boost network libraries makes Windows.h functions undefined

本文关键字:函数 未定义 Windows Boost 网络 包括      更新时间:2023-10-16

我正在尝试使用boost

我的include是这样的:

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

我正在试着测试这个呼叫:

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

当我编译我得到"ShellExecute是未定义的"。如果我将Windows.h包含移动到boost包含之上,它会被编译器拾取,但我会得到大量的Winsock错误。我使用的是VS 2015。

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

Windows.h

提供的所有函数都会发生这种情况

任何想法都会很感激!

在包含Windows.h之前需要#define WIN32_LEAN_AND_MEAN,以防止它在Windows Sockets 1标头中拖动(参见使用Windows标头)。这些头与boost asio使用的Windows Sockets 2头冲突。

下面修复了Windows Sockets声明冲突的问题:

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

注意:请记住,定义WIN32_LEAN_AND_MEAN预处理器符号会跳过包含许多系统头文件。为了解释这一点,您需要显式地包含它们。在ShellExecute的情况下,这将是#include <Shellapi.h>