236

Enter image description here

It is unclear to me how the compiler will automatically know to compile for 64-bit when it needs to. How does it know when it can confidently target 32-bit?

I am mainly curious about how the compiler knows which architecture to target when compiling. Does it analyze the code and make a decision based on what it finds?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Aaron
  • 10,386
  • 13
  • 37
  • 53
  • 2
    http://stackoverflow.com/questions/7508965/what-does-the-prefer-32-bit-compiler-flag-mean-for-visual-studio-11-managed-ap – Marc Gravell Aug 22 '12 at 05:19
  • Ah, thanks. Didn't see that before. I am still curious as to how the compiler automatically knows which architecture to target. Any ideas? – Aaron Aug 22 '12 at 05:23

4 Answers4

242

Microsoft has a blog entry What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11:

In .NET 4.5 and Visual Studio 11 the cheese has been moved. The default for most .NET projects is again AnyCPU, but there is more than one meaning to AnyCPU now. There is an additional sub-type of AnyCPU, “Any CPU 32-bit preferred”, which is the new default (overall, there are now five options for the /platform C# compiler switch: x86, Itanium, x64, anycpu, and anycpu32bitpreferred). When using the "Prefer 32-Bit" flavor of AnyCPU, the semantics are as follows:

  • If the process runs on a 32-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on a 64-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on an ARM Windows system, it runs as a 32-bit process. IL is compiled to ARM machine code.

The difference, then, between “Any CPU 32-bit preferred” and “x86” is only this: a .NET application compiled to x86 will fail to run on an ARM Windows system, but an “Any CPU 32-bit preferred” application will run successfully.

Sylvain Rodrigue
  • 4,751
  • 5
  • 53
  • 67
Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • 12
    +1. Also, the "Prefer 32-bit" checkbox is only enabled for .NET 4.5+ executable projects. – Lee Grissom May 17 '13 at 19:37
  • 15
    Another advantage of anycpu32bitspreferred is that another .exe running in 64 bits can load that assembly. – Bruno Martinez Jun 24 '13 at 14:45
  • 34
    Personally I think it is horrible they set this by default with no Tools setting to turn it off. Even worse, you can't search for it since not in the csproj files unless turned off! Probably added because of the Office Automation incompatibilities with CPUAny on a x64 machine with most folks installing 32 bit Office. – Dave Oct 31 '13 at 16:16
  • Wait, so there is no configuration that will lead to a 64-bit process? – Brian David Berman Aug 04 '16 at 13:32
  • 7
    @BrianDavidBerman there is, if you set false to 32-but preferred and set x64 or Any CPU on a 64-bit machine. – Lex Li Aug 04 '16 at 14:49
  • 1
    There are performance implications to running as 32 bit on a 64 bit operating systems. I've noticed double and long operations taking a lot longer for example. – jjxtra Aug 06 '17 at 18:06
  • 12
    The difference between x86 and Any CPU 32 bit preferred is that in the latter case the largeaddressaware flag is set on the executable. This means that the 32 bit process running on a 64 bit OS can use 2GB of memory in x86 mode and 4GB of memory in Any CPU 32 bit preferred mode. – Nic Sep 06 '17 at 09:17
  • 2
    As a followup to @Nic's statement, Here's a post with more information about how "prefer 32 bit" sets `IMAGE_FILE_LARGE_ADDRESS_AWARE` : ["Why does 'Any CPU (prefer 32-bit)' allow me to allocate more memory than x86 under .NET 4.5?"](https://stackoverflow.com/q/35068309/4975230) – jrh Jul 17 '18 at 17:38
  • 3
    Changed the default to Prefer 32-bit is a HUGE regression. – Shiv Sep 18 '19 at 23:37
27

Here's a simple answer:

Application arch.

Note: AnyCPU-32bitPreferred is only available in .Net version 4.5 and higher.

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
  • 5
    What's the difference between "runs as 32-bit" vs. runs as "WoW64". I thought WoW64 = "Windows (32-bit) on Windows64" and was needed for any 32-bit application to run. – Peter Cordes Nov 23 '19 at 21:00
  • 2
    Is there a source to this? Seemingly everywhere else still tells me the default is anycpu32bitpreferred, which is a vast difference to people running on 64bit windows machines (that's a lot). – Ran Sagy Dec 25 '19 at 12:34
  • @RanSagy you can simply test it by creating a new project and checking `Project -> Properties -> Build tab -> Platform target`... but note that `AnyCPU-32bitPreferred` is only available in .Net version 4.5 and higher. That's why the default is `AnyCPU`. – Yousha Aleayoub Dec 25 '19 at 15:08
  • In some cases mine was greyed out; I was just hoping there's some documentation on what happens in .net 4.5+ or .net standard/core (or really, MSBuild 16) – Ran Sagy Dec 25 '19 at 20:04
0

When I had 32 bit preferred checked, when run on our server, it was trying to use the 32 bit db driver instead of 64 bit that we had installed, so it wasn't connecting to the db when we ran it, so queries were failing because of failure to connect.

Michele
  • 3,617
  • 12
  • 47
  • 81
-1

The reason is: in case you don't want to use more memory with 64 bit applicatios. Which means, if your application is AnyCPU, you want to run as 32 bit.

To add more, the setting in Visual Studio targets the particular CLR:

Visual Studio installs the 32-bit version of the CLR on an x86 computer, and both the 32-bit version and the appropriate 64-bit version of the CLR on a 64-bit Windows computer. (Because Visual Studio is a 32-bit application, when it is installed on a 64-bit system, it runs under WOW64.)

Please refer to the article 64-bit Applications (MSDN).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peru
  • 2,871
  • 5
  • 37
  • 66
  • 1
    I'm not sure that's accurate. As, it's my understanding that .NET executables regardless of 32 or 64 are still limited around 2 GB per process. – JP Richardson Aug 22 '12 at 05:26
  • Any idea on how the compiler knows which architecture to target? – Aaron Aug 22 '12 at 05:28
  • 1
    Edited my answer. But not Sure if this is what you are looking for :) – Peru Aug 22 '12 at 05:48
  • 2
    @Aaron, compiler essentially sets flag for runtime to decide if it is ok to load assembly (i.e. block x86-only assembly to be loaded in x64 process) and how to start the process (for new EXE) based on flags. I believe IL is the same for both flavors. – Alexei Levenkov Aug 22 '12 at 05:51
  • 1
    @JPRichardson Yes you are right. But in .net 4.5 you have the option to increase the size. refer [MSDN](http://msdn.microsoft.com/en-us/library/hh285054.aspx) – Peru Aug 22 '12 at 05:52
  • 43
    @JPRichardson, neither 32 nor 64 bit .Net executable limited to 2GB per process - first of all per-process address space is OS level restriction (2/3+GB for 32bit process and much more for 64bit), second even 32bit version can use more than 2GB if "LargeAddressAware" flag is set on the executable. The only 2GB restrictions I know are about array/allocation sizes that are limited by Int32 range (about 2GB). – Alexei Levenkov Aug 22 '12 at 05:56
  • 3
    There are performance implications to running as 32 bit on a 64 bit operating systems. I've noticed double and long operations taking a lot longer for example. – jjxtra Aug 06 '17 at 18:06