How do I fix the Visual Studio compile error, "mismatch between processor architecture"?
This warning seems to have been introduced with the new Visual Studio 11 Beta and .NET 4.5, although I suppose it might have been possible before.
First, it really is just a warning. It should not hurt anything if you are just dealing with x86 dependencies. Microsoft is just trying to warn you when you state that your project is compatible with "Any CPU" but you have a dependency on a project or .dll assembly that is either x86 or x64. Because you have an x86 dependency, technically your project is therefore not "Any CPU" compatible. To make the warning go away, you should actually change your project from "Any CPU" to "x86". This is very easy to do, here are the steps.
- Go to the Build|Configuration Manager menu item.
- Find your project in the list, under Platform it will say "Any CPU"
- Select the "Any CPU" option from the drop down and then select
<New..>
- From that dialog, select x86 from the "New Platform" drop down and make sure "Any CPU" is selected in the "Copy settings from" drop down.
- Hit OK
- You will want to select x86 for both the Debug and Release configurations.
This will make the warning go away and also state that your assembly or project is now no longer "Any CPU" compatible but now x86 specific. This is also applicable if you are building a 64 bit project that has an x64 dependency; you would just select x64 instead.
One other note, projects can be "Any CPU" compatible usually if they are pure .NET projects. This issue only comes up if you introduce a dependency (3rd party dll or your own C++ managed project) that targets a specific processor architecture.
This is a very stubborn warning and while it is a valid warning there are some cases where it cannot be resolved due to use of 3rd party components and other reasons. I have a similar issue except that the warning is because my projects platform is AnyCPU and I'm referencing an MS library built for AMD64. This is in Visual Studio 2010 by the way, and appears to be introduced by installing the VS2012 and .Net 4.5.
Since I can't change the MS library I'm referencing, and since I know that my target deployment environment will only ever be 64-bit, I can safely ignore this issue.
What about the warning? Microsoft posted in response to a Connect report that one option is to disable that warning. You should only do this is you're very aware of your solution architecture and you fully understand your deployment target and know that it's not really an issue outside the development environment.
You can edit your project file and add this property group and setting to disable the warning:
<PropertyGroup>
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
</PropertyGroup>
A good rule of thumb is "open DLLs, closed EXEs", that is:
- EXE targets the OS, by specifying x86 or x64.
- DLLs are left open (i.e., AnyCPU) so they can be instantiated within a 32-bit or a 64-bit process.
When you build an EXE as AnyCPU, all you're doing is deferring the decision on what process bitness to use to the OS, which will JIT the EXE to its liking. That is, an x64 OS will create a 64-bit process, an x86 OS will create an 32-bit process.
Building DLLs as AnyCPU makes them compatible to either process.
For more on the subtleties of assembly loading, see here. The executive summary reads something like:
- AnyCPU – loads as x64 or x86 assembly, depending on the invoking process
- x86 – loads as x86 assembly; will not load from an x64 process
- x64 – loads as x64 assembly; will not load from an x86 process