ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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"?

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?

One more thing to consider when deciding which platform target to use: if the target of the Startup project is Any CPU and you're runnning on a 64 bit OS, you lose the ability to Edit and continue while debugging. (You're effectively debugging a 64bit process). You could make the Startup project target x86 to circumvent this while debugging. (The assemblies referenced from the startup project may continue to target Any CPU.
@CristiDiaconescu With VS2013 Edit and continue is now possible
I think there should be some note here about whether the project is an application or a class library as setting the target bitness for the latter can impact its availability to consuming applications depending on the platform. I ran into this with an x86 library being consumed by an AnyCPU application where I had to set Prefer 32-bit to avoid a load error.

P
Peter Mortensen

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

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.


so, how do I produce assembly which will JIT to x64 in C++?
C++ projects compile to native code, so the JIT compiler is not involved ... thus, you can't do what you are asking.
Some info from MS at this link: msdn.microsoft.com/en-us/library/zekwfyz4.aspx
Hi Anthony, please update your answer w.r.t. the 32-bit preferred option. This answer and this answer contain up-to-date information.
This answer is no longer correct. The meaning of AnyCPU has changed several times over the years.
P
Peter Mortensen

I think most of the important stuff has been said, but I just thought I'd add one thing: If you compile as Any CPU and 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.

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.

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:

/CLRIMAGETYPE (Specify Type of CLR Image)

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

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.


Perhaps you can edit your answer to say how one can determine if a given DLL is 32-bit only. As far as I know, this should figure that out. I think we're hoping for DLLs which are also "Any CPU", rather than just x86 only.
+1 an important distinction. Was required to use a 32 bit dependency (which wasn't identified as such). Couldn't figure out the cryptic runtime error messages. On a hunch changed the cpu target and it worked but went searching for "why". Will be nice someday when everything is 64 bit and the incompatibility issues will seem quaint like 16bit vs 32bit is now.
@GeraldDavis - I agree. The irony is there is no technological reason for not being able to mix 32-bit and 64-bit dependencies (just the lack of a thunking layer in the CLR) and I was disappointed back in early days of .NET when I saw bit-ness was still something to consider when deploying (considering this is a VM / JIT it would have been an opportunity to provide a little more added value).
@mrjoltcola: Even worse than that is the way that Microsoft decided for reason I can't fathom that registry entries should be split into a 32-bit and 64-bit universe even if they control things like screen colors, default settings, etc.
This is the answer that I was looked for... Thanks!
P
Peter Mortensen

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

https://i.stack.imgur.com/xzfCG.png


This is the most accurate answer as of 09/06/2018 and VS 15.8.0
P
Peter Mortensen

Here's a quick overview that 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."

The reason for this is sometimes you can get some DLL files that collide or some code that winds up crashing WoW in 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.


Which could be terrible if your writing for a server environment and want your application to be able to use more then 2GB of memory. You are also opting out of any x64 JIT optimizations that may someday come down the pipe.
The amount of runtime issues I've had with AnyCPU compilations is all the justification I've needed to stop using it as a build option unless someone has explicitly requested binaries that work on both. I haven't had anyone request x86 binaries over x64 for anything in over 10 years.
"By specifically specifying x86, the x64 OS will treat the app as a pure x86 app and make sure everything runs smoothly." - Sorry, I disagree. x64 OS will still run your x86 app inside a WOW64
This is just bad advice to someone who don't fully understand the impact it'll have. @AustinHarris gives a great example. Imagine a web worker process limited to only a few GBs of RAM (I recently had to deal with this in production).
W
Wai Ha Lee

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

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.

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


P
Peter Mortensen

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

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

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

dumpbin YourProgram.exe /headers

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


If it is built in "any cpu", it will show up as 32bit in dumpbin headers.
A
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.

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


+1 for answering the one part of the question that no one else did (about C++ projects not having AnyCPU as an option).
C++/CLI can be compiled to IL code without any machine code (/clr:pure). But sizeof(void*) still needs to be a compile-time constant in C++; so even when there's no machine code involved, you still can't create a binary that will work on 32-bit and 64-bit at the same time.
P
Peter Mortensen

I recommend reading this post.

When using AnyCPU, the semantics are the following:

If the process runs on a 32-bit Windows system, it runs as a 32-bit process. CIL is 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.


Only if "Prefer 32-bit" is selected.
Which is the default since Visual Studio 11
@Moerwald I believe that was a bug which has been fixed. If you read the post mamczas makes reference to, the author writes "in the current Visual Studio UI “Prefer 32-bit” is grayed out and unchecked, where in actuality it is enabled…"; In my version of VS (15.8.0) the option is still greyed out and unchecked, however, it works as expected (flag 32BITPREF = FALSE in the compiled assembly's CorFlags section)