views:

8010

answers:

6

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

Does anyone have a clear understanding what does "Any CPU" compilation target is and what sort of files it generates? I examined the output executable of this "Any CPU" build and found that they are (who would not see that coming!) the x86 executables. So, is there any the 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 option. I'm wondering why is that. Does that mean that my suspicion about "Any CPU" executables being plain 32-bit ones is right?

+17  A: 

An AnyCPU assembly will JIT to 64 bit code when loaded into 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.

AnthonyWJones
so, how do I produce assembly which will JIT to x64 in C++?
galets
C++ projects compile to native code, so the JIT compiler is not involved ... thus, you can't do what you are asking.
cplotts
@cplotts: since @galets asked this question 3 months ago its unlikely he'll see your answer. Use the @galets prefix in your comment similar to how I have here so that he gets an alert about your answer.
AnthonyWJones
thanks @AnthonyWJones. didn't know about that feature.
cplotts
+14  A: 

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 applications, 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 DLLs that collide or some code that winds up crashing WOW in the x64 environment. By specifically specifying x86, the x64 OS will treat the app as a pure x86 app and make sure everything runs smoothly.

Dillie-O
+8  A: 

"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 AnyCPU: on a x64 system, your executable compiled for X86 will run as a 32 executable.

As far as your suspicions, just go to the VS2008 command line and run the following:

dumpbin YourProgram.exe /headers

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

AngryHacker
+1  A: 

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.

Adam Tegen
+1 for answering the one part of the question that no one else did (about C++ projects not having AnyCPU as an option).
cplotts
+8  A: 

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 dlls, because your app wasn't started in WOW64, but those dlls need to run there. If you compile as x86, then the x64 system will run you app in WOW64, and you'll be able to load 32-bit dlls. 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:

http://msdn.microsoft.com/en-us/library/31zwwc39.aspx

Paul A Jungwirth