简体   繁体   English

Visual Studio 中“首选 32 位”设置的目的是什么?它实际上是如何工作的?

[英]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 位首选”应用程序将成功运行。

Here's a simple answer:这是一个简单的答案:

应用拱。

Note: AnyCPU-32bitPreferred is only available in .Net version 4.5 and higher.注意: AnyCPU-32bitPreferred 仅在 .Net 4.5 及更高版本中可用。

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.

相关问题 “首选 32 位”编译器标志对 Visual Studio(C#、VB)意味着什么? - What does the “Prefer 32-bit” compiler flag mean for Visual Studio (C#, VB)? 为什么在Visual Studio 2012中禁用了“首选32位”复选框? - Why is the checkbox 'Prefer 32-bit' disabled in Visual Studio 2012? Visual Studio 中的“首选 32 位”在控制台应用程序 (.NET Core) 下生成 64 位代码 - 'Prefer 32-bit' in Visual Studio Generates 64-bit Code Under Console App (.NET Core) 为什么更偏爱的32位标志会导致读取System32的权限问题 - Why does the prefer 32-bit flag cause a permissions issue reading system32 如何检查是否使用 Visual Studio 变量切换了“32 位首选” - How to check if '32-bit prefered' is toggled using visual studio variables 如何使用 Visual Studio 2022 注册 32 位 COM 互操作 - How to register for 32-Bit COM interop using Visual Studio 2022 禁用“首选 32 位”的目标“任何 CPU”无论如何都会使用 32 位首选项编译 - Target "Any CPU" with "Prefer 32-bit" disabled gets compiled with 32-bit preference anyway 使用Visual Studio 2010时,32位和64位操作系统之间的性能差异? - Performance difference between 32-bit and 64-bit OS when i use Visual Studio 2010? 为什么'任何CPU(更喜欢32位)'允许我在.NET 4.5下分配比x86更多的内存? - Why does 'Any CPU (prefer 32-bit)' allow me to allocate more memory than x86 under .NET 4.5? 在Visual C#2008 Express Edition中设置32位x86构建目标? - Setting 32-bit x86 build target in Visual C# 2008 Express Edition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM