C++ 致命错误:Visual Studio 中的“无目标架构”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4845198/
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
Fatal error: "No Target Architecture" in Visual Studio
提问by philipvr
When I try to compile my c++ project using Visual Studio 2010 in either Win32 or x64 mode I get the following error:
当我尝试在 Win32 或 x64 模式下使用 Visual Studio 2010 编译我的 C++ 项目时,出现以下错误:
>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winnt.h(135): fatal error C1189: #error : "No Target Architecture"
>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winnt.h(135): fatal error C1189: #error : "No Target Architecture"
My preprocessor definitions say WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
我的预处理器定义说 WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
What is causing this error and how do I fix it?
是什么导致了这个错误,我该如何解决?
// winnt.h: lines 127-136, MSVS says this is an inactive preprocessor block
#if defined(_WIN64)
#if defined(_AMD64_)
#define PROBE_ALIGNMENT( _s ) TYPE_ALIGNMENT( DWORD )
#elif defined(_IA64_)
#define PROBE_ALIGNMENT( _s ) (TYPE_ALIGNMENT( _s ) > TYPE_ALIGNMENT( DWORD ) ? \
TYPE_ALIGNMENT( _s ) : TYPE_ALIGNMENT( DWORD ))
#else
#error "No Target Architecture"
#endif
Update: I created a new msvs project and copied my code to it. I no longer have error : "No Target Architecture"
, but now I have a bunch of compile errors involving winnt.h and winbase.h and no compile errors involving any of my files. Is it possible these files are corrupted? Do I need to reinstall MSVS 2010?
更新:我创建了一个新的 msvs 项目并将我的代码复制到其中。我不再有error : "No Target Architecture"
,但现在我有一堆涉及 winnt.h 和 winbase.h 的编译错误,并且没有涉及我的任何文件的编译错误。这些文件是否可能已损坏?我是否需要重新安装 MSVS 2010?
Update 2: So I narrowed down my problem and found that it is #include <WinDef.h>
that is causing all of my compile errors with winnt.h but I still don't know how to fix it.
更新 2:所以我缩小了我的问题范围,发现它#include <WinDef.h>
导致了我所有的 winnt.h 编译错误,但我仍然不知道如何解决它。
回答by philipvr
Use #include <windows.h>
instead of #include <windef.h>
.
使用#include <windows.h>
代替#include <windef.h>
。
From the windows.h
wikipedia page:
从windows.h
维基百科页面:
There are a number of child header files that are automatically included with
windows.h
. Many of these files cannot simply be included by themselves (they are not self-contained), because of dependencies.
有许多子头文件会自动包含在
windows.h
. 由于依赖关系,其中许多文件不能简单地由它们自己包含(它们不是自包含的)。
windef.h
is one of the files automatically included with windows.h
.
windef.h
是自动包含在windows.h
.
回答by Nathan Reed
Another cause of this can be including a header that depends on windows.h
, before including windows.h
.
另一个原因可能是windows.h
在包含之前包含依赖于 的标头windows.h
。
In my case I included xinput.h
before windows.h
and got this error. Swapping the order solved the problem.
在我的情况下,我xinput.h
之前包含windows.h
并收到此错误。交换顺序解决了问题。
回答by engf-010
_WIN32 identifier is not defined.
_WIN32 标识符未定义。
use #include <SDKDDKVer.h>
用 #include <SDKDDKVer.h>
MSVS generated projects wrap this include by generating a local "targetver.h"
which is included by "stdafx.h"
that is comiled into a precompiled-header through "stdafx.cpp"
.
MSVS 生成的项目通过生成一个本地"targetver.h"
文件来包装此包含,该本地"stdafx.h"
文件通过"stdafx.cpp"
.
EDIT : do you have a /D "WIN32" on your commandline ?
编辑:你的命令行上有 /D "WIN32" 吗?
回答by David Heffernan
It would seem that _AMD64_
is not defined, since I can't imagine you are compiling for Itanium (_IA64_
).
似乎_AMD64_
没有定义,因为我无法想象您正在为 Itanium ( _IA64_
)进行编译。
回答by Michael Haephrati
Solve it by placing the following include files and definition first:
通过首先放置以下包含文件和定义来解决它:
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
回答by Melardev
If you are using Resharper make sure it does not add the wrong header for you, very common cases with ReSharper are:
- #include <consoleapi2.h
- #include <apiquery2.h>
如果您使用ReSharper的确保它不会加错头的你,与ReSharper的很常见的情况是:
- #include <consoleapi2.h
-#include <apiquery2.h>
UPDATE:
Another suggestion is to check if you are including a "partial Windows.h", what I mean is that if you include for example winbase.h or minwindef.h you may end up with that error, add "the big" instead Windows.h. There are also some less obvious cases that I went through, the most notable was when I only included synchapi.h, the docs clearly states that is the header to be included for some functions like AcquireSRWLockShared but it triggered the No target architecture, the fix was to remove the synchapi.h and include "the big" Windows.h.
The Windows.h is hughe, it defines macros(many of them remove the No target arch error) and include many other headers. In summary, always check if you are including some header that could be replaced by Windows.h.
更新:
另一个建议是检查您是否包含“部分 Windows.h”,我的意思是,如果您包含例如 winbase.h 或 minwindef.h,您最终可能会遇到该错误,请添加“大”窗户.h. 我还经历了一些不太明显的情况,最值得注意的是当我只包含 synchapi.h 时,文档明确指出这是为某些函数(如 AcquireSRWLockShared)包含的标头,但它触发了无目标架构,修复是删除synchapi.h并包含“大”Windows.h。Windows.h 很不错,它定义了宏(其中许多删除了 No target arch 错误)并包含许多其他头文件。总之,始终检查您是否包含一些可以被 Windows 替换的标题。
回答by Laurie Stearn
Another reason for the error (amongst many others that cropped up when changing the target build of a Win32 project to X64) was not having the C++ 64 bit compilers installed as noted at the top of this page.
Further to philipvr's comment on child headers, (in my case) an explicit include of winnt.hbeing unnecessarywhen windows.hwas being used.
错误的另一个原因(在将 Win32 项目的目标版本更改为 X64 时出现的许多其他错误)是没有安装 C++ 64 位编译器,如本页顶部所述。
除了 philipvr 对子标题的评论之外,(在我的情况下)明确包含winnt.h在使用windows.h时是不必要的。
回答by MxNx
I had a similar problem. In my case, I had accidentally included winuser.h
before windows.h
(actually, a buggy IDE extension had added it). Removing the winuser.h
solved the problem.
我有一个类似的问题。就我而言,我不小心包括winuser.h
之前windows.h
(实际上,一个有错误的IDE扩展已添加的话)。删除winuser.h
解决了问题。
回答by Shital Shah
Besides causes described already, I received this error because I'd include:
除了已经描述的原因之外,我收到了这个错误,因为我将包括:
#include <fileapi.h>
Apparently it was not needed (despite of CreateDirectoryW call). After commenting out, compiler was happy. Very strange.
显然不需要它(尽管有 CreateDirectoryW 调用)。注释掉后,编译器很高兴。很奇怪。
回答by Andrea Araldo
At the beginning of the file you are compiling, before any include
, try to put ONE of these lines
在您正在编译的文件的开头,在 any 之前include
,尝试放置其中一行
#define _X86_
#define _AMD64_
#define _ARM_
Choose the appropriate, only one, depending on your architecture.
根据您的架构,选择合适的,只有一个。