.net Visual Studio“任何 CPU”目标是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/516730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 11:12:04  来源:igfitidea点击:

What does the Visual Studio "Any CPU" target mean?

.netvisual-studio-2008build64-bit

提问by galets

I have some confusion related to the .NET platform build options in Visual Studio 2008.

我对 Visual Studio 2008 中的 .NET 平台构建选项有一些困惑。

What is the "Any CPU" compilation target, and what sort of files does it generate? I examined the output executable of this "Any CPU" build and found that they are the x86 executables (who would not see that coming!). So, is there any difference between targeting executable to x86 vs. "Any CPU"?

什么是“Any CPU”编译目标,它生成什么样的文件?我检查了这个“Any CPU”构建的输出可执行文件,发现它们是 x86 可执行文件(谁不会看到它的到来!)。那么,将可执行文件定位到 x86 与“任何 CPU”之间有什么区别吗?

Another thing that I noticed, is that managed C++ projects do not have this platform as an option. Why is that? Does that mean that my suspicion about "Any CPU" executables being plain 32-bit ones is right?

我注意到的另一件事是托管 C++ 项目没有这个平台作为选项。这是为什么?这是否意味着我对“任何 CPU”可执行文件是普通 32 位可执行文件的怀疑是正确的吗?

采纳答案by AnthonyWJones

An AnyCPUassembly will JIT to 64-bit code when loaded into a 64-bit process and 32 bit when loaded into a 32-bit process.

一个AnyCPU当被加载到64位的处理,并且当被加载到32位进程32钻头组件将JIT到64位的代码。

By limiting the CPU you would be saying: There is something being used by the assembly (something likely unmanaged) that requires 32 bits or 64 bits.

通过限制 CPU,您会说:程序集使用了一些需要 32 位或 64 位的东西(可能是不受管理的东西)。

回答by Paul A Jungwirth

I think most of the important stuff has been said, but I just thought I'd add one thing: If you compile as Any CPUand run on an x64 platform, then you won't be able to load 32-bit DLL files, because your application wasn't started in WoW64, but those DLL files need to run there.

我想大部分重要的东西已经说了,但我只是想补充一点:如果你编译为Any CPU并在 x64 平台上运行,那么你将无法加载 32 位 DLL 文件,因为您的应用程序不是在WoW64 中启动的,但是那些 DLL 文件需要在那里运行。

If you compile as x86, then the x64 system will run your application in WoW64, and you'll be able to load 32-bit DLL files.

如果您编译为 x86,那么 x64 系统将在 WoW64 中运行您的应用程序,并且您将能够加载 32 位 DLL 文件。

So I think you should choose "Any CPU" if your dependencies can run in either environment, but choose x86 if you have 32-bit dependencies. This article from Microsoft explains this a bit:

所以我认为如果您的依赖项可以在任一环境中运行,您应该选择“任何 CPU”,但如果您有 32 位依赖项,则选择 x86。微软的这篇文章对此进行了一些解释:

/CLRIMAGETYPE (Specify Type of CLR Image)

/CLRIMAGETYPE(指定 CLR 图像的类型)

Incidentally, this other Microsoft documentationagrees that x86 is usually a more portable choice:

顺便说一句,其他 Microsoft 文档也同意 x86 通常是更便携的选择:

Choosing x86 is generally the safest configuration for an app package since it will run on nearly every device. On some devices, an app package with the x86 configuration won't run, such as the Xbox or some IoT Core devices. However, for a PC, an x86 package is the safest choice and has the largest reach for device deployment. A substantial portion of Windows 10 devices continue to run the x86 version of Windows.

选择 x86 通常是应用程序包最安全的配置,因为它几乎可以在所有设备上运行。在某些设备上,x86 配置的应用程序包将无法运行,例如 Xbox 或某些 IoT Core 设备。但是,对于 PC 而言,x86 软件包是最安全的选择,并且具有最大的设备部署范围。大部分 Windows 10 设备继续运行 x86 版本的 Windows。

回答by Dillie-O

Here's a quick overviewthat explains the different build targets.

这是一个快速概述,解释了不同的构建目标。

From my own experience, if you're looking to build a project that will run on both x86 and x64 platforms, and you don't have any specific x64 optimizations, I'd change the build to specifically say "x86."

根据我自己的经验,如果您希望构建一个可以在 x86 和 x64 平台上运行的项目,并且您没有任何特定的 x64 优化,我会将构建更改为专门说“x86”。

The reason for this is sometimes you can get some DLL files that collide or some code that winds up crashing WoWin the x64 environment. By specifically specifying x86, the x64 OS will treat the application as a pure x86 application and make sure everything runs smoothly.

这样做的原因是有时您可能会遇到一些冲突的 DLL 文件或一些最终导致在 x64 环境中使WoW崩溃的代码。通过专门指定 x86,x64 操作系统会将应用程序视为纯 x86 应用程序,并确保一切顺利运行。

回答by DCNYAM

Check out the article Visual Studio .NET Platform Target Explained.

查看文章Visual Studio .NET 平台目标解释

The default setting, "Any CPU", means that the assembly will run natively on the CPU it is currently running on. Meaning, it will run as 64-bit on a 64-bit machine and 32-bit on a 32-bit machine. If the assembly is called from a 64-bit application, it will perform as a 64-bit assembly and so on.

默认设置“任何 CPU”意味着程序集将在它当前运行的 CPU 上本地运行。这意味着,它将在 64 位机器上以 64 位运行,在 32 位机器上以 32 位运行。如果从 64 位应用程序调用程序集,它将作为 64 位程序集执行,依此类推。

The above link has been reported to be broken, so here is another article with a similar explanation: What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11

上面的链接已经被举报失效了,所以这里是另一篇有类似解释的文章:What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11

回答by Ivan

Credit to the book "CLR via C#", see this:

归功于“CLR via C#”一书,请参阅:

Enter image description here

在此处输入图片说明

回答by AngryHacker

"Any CPU" means that when the program is started, the .NET Framework will figure out, based on the OS bitness, whether to run your program in 32 bits or 64 bits.

“任何 CPU”意味着当程序启动时,.NET Framework 将根据操作系统位数确定以 32 位还是 64 位运行您的程序。

There is a difference between x86and Any CPU: on a x64 system, your executable compiled for X86 will run as a 32-bit executable.

x86Any CPU之间存在差异:在 x64 系统上,为 X86 编译的可执行文件将作为 32 位可执行文件运行。

As far as your suspicions go, just go to the Visual Studio 2008 command line and run the following.

就您的怀疑而言,只需转到 Visual Studio 2008 命令行并运行以下命令。

dumpbin YourProgram.exe /headers

It will tell you the bitness of your program, plus a whole lot more.

它会告诉您程序的位数,以及更多信息。

回答by Adam Tegen

Any CPU means that it will work on any platform. This is because managed code is similar to Java. Think of it as being compiled to a byte code that is interpreted by the .NET Framework at run-time.

任何 CPU 都意味着它可以在任何平台上工作。这是因为托管代码类似于 Java。将其视为被编译为由 .NET Framework 在运行时解释的字节代码。

C++ does not have this option because it is compiled to machine code that is platform specific.

C++ 没有这个选项,因为它被编译为特定于平台的机器代码。

回答by mamczas

I recommend reading this post.

我建议阅读这篇文章

When using AnyCPU, the semantics are the following:

使用AnyCPU 时,语义如下:

  • If the process runs on a 32-bit Windows system, it runs as a 32-bit process. CILis compiled to x86 machine code.
  • If the process runs on a 64-bit Windows system, it runs as a 32-bit process. CIL is compiled to x86 machine code.
  • If the process runs on an ARM Windows system, it runs as a 32-bit process. CIL is compiled to ARM machine code.
  • 如果进程在 32 位 Windows 系统上运行,则它作为 32 位进程运行。CIL被编译为 x86 机器码。
  • 如果进程在 64 位 Windows 系统上运行,则它作为 32 位进程运行。CIL 被编译为 x86 机器码。
  • 如果进程在 ARM Windows 系统上运行,则它作为 32 位进程运行。CIL 被编译为 ARM 机器码。

回答by M. Fawad Surosh

This is how I did it in Visual Studio 2017:

这是我在 Visual Studio 2017 中的做法:

  • In the Solution explorer
  • Right-click on your project
  • Click on the "Properties"
  • Click on the "Build"
  • Turn off the option "Prefer 32-bit"
  • And you can choose "x64" from the Platform target.
  • 在解决方案资源管理器中
  • 右键单击您的项目
  • 点击“属性”
  • 点击“构建”
  • 关闭“首选 32 位”选项
  • 您可以从平台目标中选择“x64”。