我不清楚编译器如何在需要时自动知道编译64位。它如何知道什么时候可以自信地瞄准32位?
我主要好奇的是编译器在编译时如何知道要针对哪个体系结构。它是否分析代码并根据发现的内容做出决定?
我不清楚编译器如何在需要时自动知道编译64位。它如何知道什么时候可以自信地瞄准32位?
我主要好奇的是编译器在编译时如何知道要针对哪个体系结构。它是否分析代码并根据发现的内容做出决定?
当前回答
微软有一篇博文,关于AnyCPU在。net 4.5和Visual Studio 11的真正含义:
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 the "Prefer 32-Bit" flavor of AnyCPU, the semantics are as follows: 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. 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.
其他回答
微软有一篇博文,关于AnyCPU在。net 4.5和Visual Studio 11的真正含义:
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 the "Prefer 32-Bit" flavor of AnyCPU, the semantics are as follows: 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. 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.
原因是:如果你不想在64位应用程序中使用更多的内存。这意味着,如果您的应用程序是AnyCPU,则希望以32位运行。
为了添加更多,Visual Studio中的设置针对特定的CLR:
Visual Studio在x86计算机上安装32位版本的CLR,在64位Windows计算机上安装32位版本和适当的64位版本的CLR。(因为Visual Studio是一个32位应用程序,当它安装在64位系统上时,它在WOW64下运行。)
请参考文章64位应用程序(MSDN)。
当我有32位首选检查时,当在我们的服务器上运行时,它试图使用32位db驱动程序,而不是我们安装的64位,所以当我们运行它时,它没有连接到db,所以查询失败,因为连接失败。
这里有一个简单的答案:
注意:AnyCPU-32bitPreferred仅在. net 4.5及更高版本中可用。