Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

c# - Any CPU - Prefer 32 bit

What does the Any CPU - Prefer 32 bit option do?

While I am aware that WinRT can not handle exe and can only run Windows Store apps, there are several questions exist on StackOverflow that ask the same question and both reference this blog that says:

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 that flavor of AnyCPU, the semantics are the following:

  • 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.

However, after purchasing my Surface RT, I created a Hello World program, set it to Any CPU, checked the Prefer 32 Bit flag, compiled and copied it to my Surface. When I ran the program the OS told me that it could not run the program and that I should look to the marketplace as it would for any x86/x64 exe. The exact message displayed was: "This app can't run on your PC. To find apps for this PC, open the Windows Store."

So what does this actually do and is it possible to compile an Any CPU application for Window RT on ARM?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based solely on the quote you specified, it suggests that it means the following:

  • If the CPU supports 32-bit processing, then the final machine code will be 32-bit (ARM or x86, doesn't matter).

  • If not, then the machine code will be 64-bit.

In the days of old, AnyCPU meant "I am platform agnostic." This typically meant that you would get 64-bit on 64-bit platforms, and 32-bit otherwise.

However, there are reasons why you may want 32-bit even on a 64-bit CPU: Pointers are half the size, so you use less memory; code is smaller, so more fits into the cache, etc. But, there was no way to force the CLR to use 32-bit, and still retain 64-bit in the cases it was necessary.

Now, with AnyCPU - 32 bit preferred, you can have your cake (64-bit support when necessary) and eat it too (32-bit support when possible)

Note: If I am not mistaken, Itanium would be a platform that only supports 64-bit, not 32-bit code. There may or may not be others.

Disclaimer: I am not an expert, this is just from various blog posts that I've read over the last 6 months or so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...