C# compiling for 32/64 bit, or for any cpu?

.NetAssemblies64 Bit32bit 64bitAnycpu

.Net Problem Overview


> Possible Duplicate:
> Visual Studio “Any CPU” target

I've noticed that when compiling C# code in VS, there's 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?

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhelloworld922View Question on Stackoverflow
Solution 1 - .NetJaroslav JandekView Answer on Stackoverflow
Solution 2 - .NetGiladView Answer on Stackoverflow