本文翻译自:How do I fix the Visual Studio compile error, “mismatch between processor architecture”?
I'm new to project configuration in Visual Studio 2010, but I've done some research and still can't quite figure this issue out. 我是Visual Studio 2010中项目配置的新手,但我进行了一些研究 ,但仍然无法完全弄清楚这个问题。 I have a Visual Studio solution with a C++ DLL referencing the C# DLL. 我有一个Visual Studio解决方案,其中包含引用C#DLL的C ++ DLL。 The C# DLL references a few other DLLs, some within my project and some external. C#DLL引用了其他一些DLL,其中一些在我的项目中,而另一些在外部。 When I try to compile the C++ DLL, I get this warning: 当我尝试编译C ++ DLL时,出现以下警告:
warning MSB3270: There was a mismatch between the processor architecture of the project being build "MSIL" and the processor architecture of the reference "[internal C# dll]", "x86". 警告MSB3270:正在生成的项目“ MSIL”的处理器架构与引用“ [内部C#dll]”,“ x86”的处理器架构之间不匹配。
It tells me to go to Configuration Manager to align my architectures. 它告诉我去配置管理器来调整我的体系结构。 The C# DLL is set up with platform target x86. 使用平台目标x86设置C#DLL。 If I try to change this to something else, like Any CPU, it complains because one of the external DLLs it depends on has platform target x86. 如果我尝试将其更改为其他任何东西,例如Any CPU,它会抱怨,因为它依赖的外部DLL之一具有平台目标x86。
When I look at Configuration Manager it shows the Platform for my C# DLL as x86 and for my C++ project as Win32. 当我查看Configuration Manager时,它显示了我的C#DLL的平台为x86,以及我的C ++项目的平台为Win32。 This seems like the right setup; 这似乎是正确的设置。 surely I don't want the project for my C++ project to have platform set to x64, which is the only other option presented. 当然,我不希望我的C ++项目的项目平台设置为x64,这是唯一提供的其他选项。
What am I doing wrong here? 我在这里做错了什么?
#1楼
参考:https://stackoom.com/question/gQzU/如何解决Visual-Studio编译错误-即-处理器体系结构之间的不匹配
#2楼
For C# projects, the target of x86 does what it sounds like. 对于C#项目,x86的目标听起来像是。 It says that this assembly only supports x86 architectures. 它说该程序集仅支持x86体系结构。 Likewise for x64. 对于x64同样。 Any CPU on the other hand says that I don't care which architecture, I support both. 另一方面,任何CPU都说我不在乎哪种架构,我都支持。 So, the next 2 questions are (1) what is the configuration of the executable that uses these dlls? 因此,接下来的两个问题是(1)使用这些dll的可执行文件的配置是什么? and (2) what is the bitness of your OS/Computer? (2)您的操作系统/计算机的位数是多少? The reason I ask is because if your executable is compiled to run in 64-bit, then it NEEDS all dependencies to be able to run in 64-bit mode as well. 我问的原因是因为如果您的可执行文件被编译为可以在64位模式下运行,则它需要所有依赖项才能在64位模式下运行。 Your Any CPU assembly should be able to be loaded, but perhaps it is referencing some other dependency that is only capable of running in x86 configuration. 您的Any CPU程序集应该能够被加载,但是也许它引用了其他一些只能在x86配置中运行的依赖项。 Check all dependencies and dependencies-of-dependencies to make sure everything is either "Any CPU" or "x64" if you plan to run the executable in 64-bit mode. 如果计划以64位模式运行可执行文件,请检查所有依赖项和依赖项依赖项,以确保所有内容都是“ Any CPU”或“ x64”。 Otherwise, you'll have issues. 否则,您将遇到问题。
In many ways, Visual Studio does not make compiling a mixture of Any CPU and various architecture dependent assemblies easy. 在许多方面,Visual Studio都不容易混合任何CPU和依赖于各种体系结构的程序集。 It is doable, but it often requires that an assembly that would otherwise be "Any CPU" to have to be compiled separately for x86 and x64 because some dependency-of-a-dependency somewhere has two versions. 这是可行的,但是它通常要求必须为x86和x64分别编译否则将是“任何CPU”的程序集,因为某个地方的某些依赖项具有两个版本。
#3楼
The C# DLL is set up with platform target x86 使用平台目标x86设置C#DLL
Which is kind of the problem, a DLL doesn't actually get to choose what the bitness of the process will be. 这是一种问题,DLL实际上并没有选择进程的位数。 That's entirely determined by the EXE project, that's the first assembly that gets loaded so its Platform target setting is the one that counts and sets the bitness for the process. 这完全由EXE项目决定,这是第一个加载的程序集,因此其Platform目标设置是计算和设置进程位数的程序。
The DLLs have no choice, they need to be compatible with the process bitness. DLL没有选择,它们需要与进程位兼容。 If they are not then you'll get a big Kaboom with a BadImageFormatException when your code tries to use them. 如果不是,那么当您的代码尝试使用它们时,您会得到一个带有BadImageFormatException的大Kaboom。
So a good selection for the DLLs is AnyCPU so they work either way. 因此,DLL的一个很好的选择是AnyCPU,因此它们可以以任何一种方式工作。 That makes lots of sense for C# DLLs, they do work either way. 对于C#DLL来说,这很有意义,它们可以以任何一种方式工作。 But sure, not your C++/CLI mixed mode DLL, it contains unmanaged code that can only work well when the process runs in 32-bit mode. 但是可以肯定的是,它不是包含C ++ / CLI混合模式DLL的非托管代码,只有在进程以32位模式运行时才能正常运行。 You can get the build system to generate warnings about that. 您可以使构建系统生成有关此的警告。 Which is exactly what you got. 正是您所得到的。 Just warnings, it still builds properly. 只是警告,它仍然可以正确构建。
Just punt the problem. 只是解决问题。 Set the EXE project's Platform target to x86, it isn't going to work with any other setting. 将EXE项目的“平台”目标设置为x86,它将无法与其他任何设置一起使用。 And just keep all the DLL projects at AnyCPU. 并将所有DLL项目保留在AnyCPU中。
#4楼
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. 尽管我认为以前可能已经实现了,但新的Visual Studio 11 Beta和.NET 4.5似乎已经引入了此警告。
First, it really is just a warning. 首先,这实际上只是一个警告。 It should not hurt anything if you are just dealing with x86 dependencies. 如果您仅处理x86依赖项,那么它不会有任何伤害。 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. 当您声明您的项目与“任何CPU”兼容,但您依赖于x86或x64的项目或.dll程序集时,Microsoft只是试图警告您。 Because you have an x86 dependency, technically your project is therefore not "Any CPU" compatible. 因为您具有x86依赖性,所以从技术上讲,您的项目因此不兼容“任何CPU”。 To make the warning go away, you should actually change your project from "Any CPU" to "x86". 为了消除警告,您实际上应该将项目从“ Any CPU”更改为“ 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" 在列表中找到您的项目,在“平台”下它将显示“ Any CPU”
- Select the "Any CPU" option from the drop down and then select
<New..>
从下拉列表中选择“任何CPU”选项,然后选择<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. 在该对话框中,从“新平台”下拉列表中选择x86,并确保在“从以下位置复制设置”下拉列表中选择了“任何CPU”。
- Hit OK 点击确定
- You will want to select x86 for both the Debug and Release configurations. 您将要同时为调试和发布配置选择x86。
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. 这将使警告消失,并声明您的程序集或项目现在不再与“任何CPU”兼容,而是特定于x86。 This is also applicable if you are building a 64 bit project that has an x64 dependency; 如果您要构建具有x64依赖项的64位项目,这也适用。 you would just select x64 instead. 您只需选择x64。
One other note, projects can be "Any CPU" compatible usually if they are pure .NET projects. 另一个要注意的是,如果项目是纯.NET项目,则它们通常可以“兼容任何CPU”。 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. 仅当您引入针对特定处理器体系结构的依赖项(第三方dll或您自己的C ++托管项目)时,才会出现此问题。
#5楼
If your C# DLL has x86-based dependencies, then your DLL itself is going to have to be x86. 如果您的C#DLL具有基于x86的依赖项,则您的DLL本身将必须是x86。 I don't really see a way around that. 我真的没有办法解决。 VS complains about changing it to (for example) x64 because a 64-bit executable can't load 32-bit libraries. VS抱怨将其更改为(例如)x64,因为64位可执行文件无法加载32位库。
I'm a little confused about the configuration of the C++ project. 我对C ++项目的配置有些困惑。 The warning message that was provided for the build suggests that it was targeted for AnyCPU, because it reported the platform that it targeted was [MSIL], but you indicated that the configuration for the project was actually Win32. 为该构建提供的警告消息表明它是针对AnyCPU的,因为它报告的目标平台是[MSIL],但是您指出该项目的配置实际上是Win32。 A native Win32 app shouldn't involve the MSIL - although it would likely need to have CLR support enabled if it is interacting with a C# library. 本机Win32应用程序不应包含MSIL-尽管如果与C#库进行交互,则可能需要启用CLR支持。 So I think there are a few gaps on the information side. 因此,我认为信息方面存在一些差距。
Could I respectfully ask you review and post a bit more detail of the exact configuration of the projects and how they are inter-related? 我能否请您回顾并发布更多有关项目的确切配置以及它们如何相互关联的详细信息? Be glad to help further if possible. 如果可能,很高兴为您提供进一步的帮助。
#6楼
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. 除了警告是因为我的项目平台是AnyCPU,而且我引用的是为AMD64构建的MS库之外,我有一个类似的问题。 This is in Visual Studio 2010 by the way, and appears to be introduced by installing the VS2012 and .Net 4.5. 顺便说一下,这是在Visual Studio 2010中,并且似乎是通过安装VS2012和.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. 由于我无法更改正在引用的MS库,并且由于我知道我的目标部署环境将永远只有64位,因此我可以放心地忽略此问题。
What about the warning? 那警告呢? Microsoft posted in response to a Connect report that one option is to disable that warning. Microsoft针对Connect报告发布了一个选项,就是禁用该警告。 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>