Fatal error: "No Target Architecture" in Visual Studio

128,622

Solution 1

Use #include <windows.h> instead of #include <windef.h>.

From the windows.h wikipedia page:

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.

windef.h is one of the files automatically included with windows.h.

Solution 2

Another cause of this can be including a header that depends on windows.h, before including windows.h.

In my case I included xinput.h before windows.h and got this error. Swapping the order solved the problem.

Solution 3

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>

Solution 4

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>
  • #include <fileapi.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" Windows.h instead. There are also some less obvious cases that I went through, the most notable was when I only included synchapi.h, the docs clearly state 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 huge, it defines macros(many of them remove the No target arch error) and includes many other headers. In summary, always check if you are including some header that could be replaced by Windows.h because it is not unusual to include a header that relies on some constants that are defined by Windows.h, so if you fail to include this header your compilation may fail.

Solution 5

_WIN32 identifier is not defined.

use #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".

EDIT : do you have a /D "WIN32" on your commandline ?

Share:
128,622
philipvr
Author by

philipvr

Updated on January 05, 2021

Comments

  • philipvr
    philipvr over 3 years

    When I try to compile my c++ project using Visual Studio 2010 in either Win32 or x64 mode I get the following error:

    >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)

    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?

    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.

  • David Heffernan
    David Heffernan over 13 years
    Should it be _WIN32 rather than WIN32? It's not my area of expertise, but given that the header is looking for _WIN64, '_AMD64_` etc. it would seem plausible.
  • engf-010
    engf-010 over 13 years
    @David Heffernan : at the commandline it says WIN32 (no _) even for x84. Don't know the rationale of it (but who does understand MS)
  • David Heffernan
    David Heffernan over 13 years
    @Edwin x84? Is that George Orwell's computer?
  • engf-010
    engf-010 over 13 years
    @David Heffernan: yes ,big brother is watching me ! (obviously x64 i meant)
  • engf-010
    engf-010 over 13 years
    AMD64 will be defined under some conditions:#if !defined(68K) && !defined(MPPC) && !defined(X86) && !defined(IA64) && !defined(AMD64) && defined(_M_AMD64)
  • David Heffernan
    David Heffernan over 13 years
    @Edwin If _AMD64_ or _IA64_ was defined, then he wouldn't be getting the error. That's what the header file says.
  • engf-010
    engf-010 over 13 years
    philipvr has updated his post. He has other (more) problems. He's thinking re-install MSVS.
  • engf-010
    engf-010 over 13 years
    I did think of that ,but I couldn't imaging that you didn't include windows.h.
  • engf-010
    engf-010 over 13 years
    windows.h defines alkinds of other defines based on compiler-switches and includes many WINAPI-headers ,some of which depending on things defned by windows.h.
  • David Heffernan
    David Heffernan over 13 years
    congratulations, you have fixed your problem and you have enough rep to vote up!
  • rsethc
    rsethc about 10 years
    Neither windows.h not windowsx.h (I assume them to be the same thing but have tried both anyway) help with that #error Hey man you gotta choose a target.. What else could fix that?
  • Jens Åkerblom
    Jens Åkerblom over 7 years
    Heads up: seems including Xinput.h before Windows.h causes this too.
  • Ari Seyhun
    Ari Seyhun over 6 years
    Exactly my solution! Thanks for saving me hours of frustration.
  • AlastairG
    AlastairG over 6 years
    If I include Windows.h I get told that I cannot include it twice. Why doesn't it just ignore the second inclusion?
  • Mugen
    Mugen over 6 years
    In my case it was <windows.h> instead of <winnt.h>
  • Laurie Stearn
    Laurie Stearn about 6 years
    Another visit to this page arose when an old project happened to have ` #include <synchapi.h>` in a CriticalSection header.
  • Herve Mutombo
    Herve Mutombo almost 6 years
    In my case _WIN32 was defined and was the culprit. I was building for x64. Your answer put me on track. Good job!
  • SWdV
    SWdV over 5 years
    For me ReSharper added consoleapi2.h
  • Jonathan Lidbeck
    Jonathan Lidbeck over 4 years
    This fixed both my x86 and x64 builds. I needed to add these lines before #include <WinUser.h>.
  • JOE
    JOE over 3 years
    I have exactly the same situation.
  • keineahnung2345
    keineahnung2345 almost 3 years
    Thank you. I include windows.h to replace WinBase.h and fileapi.h.