I have a Windows Forms project that is written by C# language in Visual Studio 2010. I have to run my exe both for x86 and x64 processors. Every time i am changing the configuration to compile for both processor types. Is there any change to compile the source code at once for both x86 and x64 processor types?
-
The only way to compile for "Both" x86 and x64 is to compile for x86. x86 with run on x64. If what you want is something that will run natively in either x86 of x64, anyCPU is the recommended option. – Peter Ritchie Sep 08 '12 at 13:42
-
That would be what [Any CPU](http://stackoverflow.com/a/516740/477878) is for. – Joachim Isaksson Sep 08 '12 at 13:42
3 Answers
You should choose the AnyCPU
configuration and your app will run in 32bit if your OS is 32bit and in 64bit if the OS is 64bit. You should thank the JIT
compiler that translates your MSIL
code to the appropriate bitness required by the hosting environment.
Said that, however, your should keep in mind, if you use drivers or other libraries that are not compiled in the same mode, you will find problems in your deployment. You will need both libraries (64/32) and you need to install/deploy the correct one for the destination machine.
(Comes to mind the initial mess with ACE/Jet OleDB
providers)
-
Even Microsoft advise against using AnyCPU. Use x86 for everything unless you HAVE to use x64. – Jack Sep 10 '12 at 18:40
-
-
http://blogs.msdn.com/b/rmbyers/archive/2009/06/09/anycpu-exes-are-usually-more-trouble-then-they-re-worth.aspx // AnyCPU can bite you in many unexpected ways - avoid! Off the top of my head: you need to debug your work on x86 and x64 machines // You can't edit and continue an AnyCPU project on an x64 dev machine // There can be nasty problems related to 3rd party libraries // You may need to relicence expensive libraries // I can go on and on // x86 is the smart way to go – Jack Sep 13 '12 at 20:43
You don't need to do this, unless you're working with native code somewhere. Everything compiles down to CIL/MSIL, which is architecture/platform-independent.
As noted in the above and recent comments, use the "Any CPU" option.

- 27,712
- 8
- 86
- 94
-
It compiles down to MSIL, but the x86/x64 switch tells the assembly which platform to JIT compile to. – Peter Ritchie Sep 08 '12 at 13:43
You can use AnyCPU configuration. However, both x86 and x64 assemblies should work on both types of processors unless you are calling out to native code libraries (e.g. C, C++, Delphi). Meaning, you can use x64 .NET assemblies on x86 CPU and operating system.
For more information you should read this (or similar) article about platform targeting.

- 6,871
- 32
- 44