[英]What is the purpose of the "Prefer 32-bit" setting in Visual Studio and how does it actually work?
It is unclear to me how the compiler will automatically know to compile for 64-bit when it needs to.我不清楚编译器如何在需要时自动知道为 64 位编译。 How does it know when it can confidently target 32-bit?它如何知道何时可以自信地针对 32 位?
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?它是否会分析代码并根据发现的内容做出决定?
Microsoft has a blog entry What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11 : Microsoft 有一篇博客文章AnyCPU 在 .NET 4.5 和 Visual Studio 11 中的真正含义:
In .NET 4.5 and Visual Studio 11 the cheese has been moved.在 .NET 4.5 和 Visual Studio 11 中,奶酪已被移动。 The default for most .NET projects is again AnyCPU, but there is more than one meaning to AnyCPU now.大多数 .NET 项目的默认设置仍然是 AnyCPU,但现在 AnyCPU 的含义不止一种。 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). AnyCPU 有一个额外的子类型,“Any CPU 32-bit preferred”,这是新的默认值(总的来说,现在有五个用于 /platform C# 编译器开关的选项:x86、Itanium、x64、anycpu 和 anycpu32bitpreferred )。 When using the "Prefer 32-Bit" flavor of AnyCPU, the semantics are as follows:当使用 AnyCPU 的“Prefer 32-Bit”风格时,语义如下:
- If the process runs on a 32-bit Windows system, it runs as a 32-bit process.如果进程在 32 位 Windows 系统上运行,则它作为 32 位进程运行。 IL is compiled to x86 machine code. IL 被编译为 x86 机器码。
- If the process runs on a 64-bit Windows system, it runs as a 32-bit process.如果进程在 64 位 Windows 系统上运行,则它作为 32 位进程运行。 IL is compiled to x86 machine code. IL 被编译为 x86 机器码。
- If the process runs on an ARM Windows system, it runs as a 32-bit process.如果进程在 ARM Windows 系统上运行,则它作为 32 位进程运行。 IL is compiled to ARM machine code. IL 被编译为 ARM 机器码。
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.那么,“任何 CPU 32 位首选”和“x86”之间的区别仅在于:编译为 x86 的 .NET 应用程序将无法在 ARM Windows 系统上运行,但“任何 CPU 32 位首选”应用程序将成功运行。
The reason is: in case you don't want to use more memory with 64 bit applicatios.原因是:如果您不想在 64 位应用程序中使用更多内存。 Which means, if your application is AnyCPU , you want to run as 32 bit.这意味着,如果您的应用程序是AnyCPU ,您希望以 32 位运行。
To add more, the setting in Visual Studio targets the particular CLR:要添加更多内容,Visual Studio 中的设置针对特定的 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. Visual Studio 在 x86 计算机上安装 32 位版本的 CLR,在 64 位 Windows 计算机上安装 32 位版本和相应的 64 位版本的 CLR。 (Because Visual Studio is a 32-bit application, when it is installed on a 64-bit system, it runs under WOW64.) (因为 Visual Studio 是 32 位应用程序,所以当它安装在 64 位系统上时,它会在 WOW64 下运行。)
Please refer to the article 64-bit Applications ( MSDN ).请参阅64 位应用程序( MSDN ) 一文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.