visual-studio 处理 Winnt.h 的奇怪编译错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/257134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:50:29  来源:igfitidea点击:

Weird compile error dealing with Winnt.h

c++cvisual-studiowinapi

提问by Frank Krueger

When trying to compile a file that include winnt.h via windows.h, I get the following error:

尝试通过 windows.h 编译包含 winnt.h 的文件时,出现以下错误:

MyGl.cpp
..\microsoft sdks\windows\v6.0a\include\winnt.h(964) : error C2988: unrecognizable template declaration/definition
..\microsoft sdks\windows\v6.0a\include\winnt.h(964) : error C2059: syntax error : '&'

They point to the following lines in Winnt.h

它们指向 Winnt.h 中的以下几行

extern "C++" // templates cannot be declared to have 'C' linkage
template <typename T, size_t N>
char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N];

#define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A)))

Any ideas for what's going on?

关于发生了什么的任何想法?

My compiler:

我的编译器:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

回答by Foredecker

There are at least two ways to do this. The first is to simply include windows.hat the top of all your files. Then include winnt.honly if you need it. However, I find this a bit too much - I don't see the need of including all this goo in every single file.

至少有两种方法可以做到这一点。第一个是简单地包含windows.h在所有文件的顶部。然后winnt.h仅在需要时才包含。但是,我觉得这有点太多了 - 我认为不需要在每个文件中都包含所有这些粘性物质。

What I do is this at the very top (first thing) in my C/C++ header files.

我所做的是在我的 C/C++ 头文件的最顶部(第一件事)。

#ifndef __wtypes_h__
#include <wtypes.h>
#endif

#ifndef __WINDEF_
#include <windef.h>
#endif

This will get you you the data types, defines, and fundamental Windows API's. You may also need to add the following:

这将为您提供数据类型、定义和基本的 Windows API。您可能还需要添加以下内容:

#ifndef _WINUSER_
#include <winuser.h>
#endif

#ifndef __RPC_H__
#include <rpc.h>
#endif

WinNT is a bit of a special animal - don't include it if including the above files works for you. If you do need it, include it after wtypes.hand `windef.h'

WinNT 是一种特殊的动物 - 如果包含上述文件适合您,请不要包含它。如果您确实需要它,请将其包含在wtypes.h“windef.h”之后

If this doesn't work, then check your include paths and predefined macros to see if those might be breaking your build.

如果这不起作用,请检查您的包含路径和预定义的宏,看看它们是否会破坏您的构建。

Regards, Foredecker

问候, Foredecker