C# compiling for 32/64 bit, or for 'Any CPU'? [duplicate]
Asked Answered
L

2

81

Possible Duplicate:
What does the Visual Studio "Any CPU" target mean?

I've noticed that when compiling C# code in Visual Studio, there are typically options for compiling for 32/64 bit systems, and there's also one for compiling for Any CPU.

What's the difference between the two options? Does choosing Any CPU only compile down to an intermediate byte code while the first option compiles down to machine code (this sounds unlikely to me)? Or something else?

Lyricist answered 8/3, 2011 at 7:50 Comment(1)
Also see: Visual Studio “Any CPU” targetEntourage
F
195

On a 32-bit machine:

  • Any CPU: runs as a 32-bit process, can load Any CPU and x86 assemblies, will get BadImageFormatException if it tries to load an x64 assembly.

  • x86: same as Any CPU.

  • x64: BadImageFormatException always.

On a 64-bit machine:

  • Any CPU: runs as a 64-bit process, can load Any CPU and x64 assemblies, will get BadImageFormatException if it tries to load an x86 assembly.

  • x86: runs as a 32-bit process, can load Any CPU and x86 assemblies, will get BadImageFormatException if it tries to load an x64 assembly.

  • x64: same as Any CPU.

It is the JIT compiler that generates an assembly code that's compatible with the requested target based on this flag.

Fascinator answered 8/3, 2011 at 7:54 Comment(3)
That's a great answer covering all permutations. Just to add that this answer will determine if a DLL/assembly is 32 bit, 64 bit, or "Any CPU".Elemental
Looks like this has changed in .NET 4.5. Here's a link to the updated answer: https://mcmap.net/q/116681/-what-is-the-purpose-of-the-quot-prefer-32-bit-quot-setting-in-visual-studio-and-how-does-it-actually-workAnaesthetize
I was wondering why my AnyCPU wasn't running as 64-bit on my PC. Turns out we now have the prefer 32-bit flag. See the answer above fore more info.Demonstration
V
20

x86 - Your software will always run in 32-bit mode, both on 32-bit systems and 64-bit systems.

x64 - Your software will always run in 64-bit mode, will run on 64-bit system but won't run on 32-bit system.

Any CPU - Your software will run according to your OS. If you have a 32-bit OS you code will run in 32-bit mode. If you have a 64-bit OS your code will run in 64-bit mode.

Vc answered 8/3, 2011 at 7:54 Comment(1)
As a side note to this answer, it's just a flag in the binary, what option you choose doesn't change its contents. It can actually be modified later by using CorFlags tool.Condensate

© 2022 - 2024 — McMap. All rights reserved.