.net 如何修复 Visual Studio 编译错误“处理器架构之间不匹配”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10113532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:21:01  来源:igfitidea点击:

How do I fix the Visual Studio compile error, "mismatch between processor architecture"?

.netvisual-studio

提问by Paul Eastlund

I'm new to project configuration in Visual Studio 2010, but I've done some researchand still can't quite figure this issue out. I have a Visual Studio solution with a C++ DLL referencing the C# DLL. The C# DLL references a few other DLLs, some within my project and some external. When I try to compile the C++ DLL, I get this warning:

我是 Visual Studio 2010 中项目配置的新手,但我已经做了一些研究,但仍然无法弄清楚这个问题。我有一个带有引用 C# DLL 的 C++ DLL 的 Visual Studio 解决方案。C# DLL 引用了一些其他 DLL,一些在我的项目中,一些在外部。当我尝试编译 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. If I try to change this to something else, like Any CPU, it complains because one of the external DLLs itdepends on has platform target x86.

它告诉我去配置管理器来调整我的架构。C# DLL 设置为平台目标 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. 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# DLL 的平台显示为 x86,将我的 C++ 项目的平台显示为 Win32。这似乎是正确的设置;当然,我不希望我的 C++ 项目的项目将平台设置为 x64,这是提供的唯一其他选项。

What am I doing wrong here?

我在这里做错了什么?

回答by David Sacks

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

首先,这真的只是一个警告。如果您只是处理 x86 依赖项,它应该不会有任何伤害。当您声明您的项目与“任何 CPU”兼容但您依赖于 x86 或 x64 的项目或 .dll 程序集时,Microsoft 只是试图警告您。由于您有 x86 依赖项,因此从技术上讲,您的项目与“任何 CPU”不兼容。要使警告消失,您实际上应该将项目从“任何 CPU”更改为“x86”。这很容易做到,下面是步骤。

  1. Go to the Build|Configuration Manager menu item.
  2. Find your project in the list, under Platform it will say "Any CPU"
  3. Select the "Any CPU" option from the drop down and then select <New..>
  4. 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.
  5. Hit OK
  6. You will want to select x86 for both the Debug and Release configurations.
  1. 转到构建|配置管理器菜单项。
  2. 在列表中找到您的项目,在平台下它会显示“任何 CPU”
  3. 从下拉列表中选择“Any CPU”选项,然后选择 <New..>
  4. 在该对话框中,从“新平台”下拉菜单中选择 x86,并确保在“复制设置自”下拉菜单中选择了“任何 CPU”。
  5. 点击确定
  6. 您需要为 Debug 和 Release 配置选择 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. This is also applicable if you are building a 64 bit project that has an x64 dependency; you would just select x64 instead.

这将使警告消失,并说明您的程序集或项目现在不再与“任何 CPU”兼容,但现在特定于 x86。如果您正在构建具有 x64 依赖项的 64 位项目,这也适用;您只需选择 x64。

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.

另一个注意事项是,如果项目是纯 .NET 项目,则它们通常可以与“任何 CPU”兼容。仅当您引入针对特定处理器体系结构的依赖项(第 3 方 dll 或您自己的 C++ 托管项目)时,才会出现此问题。

回答by Gio Palacino

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.

这是一个非常顽固的警告,虽然它是一个有效的警告,但在某些情况下,由于使用 3rd 方组件和其他原因而无法解决。我有一个类似的问题,除了警告是因为我的项目平台是 AnyCPU 并且我正在引用为 AMD64 构建的 MS 库。顺便说一下,这是在 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 reportthat 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.

警告呢?微软在回应Connect 报告时发布了一个选项是禁用该警告。只有当您非常了解您的解决方案架构并且您完全了解您的部署目标并且知道这不是开发环境之外的真正问题时,您才应该这样做。

You can edit your project file and add this property group and setting to disable the warning:

您可以编辑您的项目文件并添加此属性组和设置以禁用警告:

<PropertyGroup>
  <ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
</PropertyGroup>

回答by Gustavo Mori

A good rule of thumb is "open DLLs, closed EXEs", that is:

一个好的经验法则是“打开 DLL,关闭 EXE”,即:

  • EXEtargets the OS, by specifying x86 or x64.
  • DLLsare left open (i.e., AnyCPU) so they can be instantiated within a 32-bit or a 64-bit process.
  • EXE以操作系统为目标,通过指定 x86 或 x64。
  • DLL保持打开状态(即 AnyCPU),因此它们可以在 32 位或 64 位进程中实例化。

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.

当您将 EXE 构建为 AnyCPU 时,您所做的就是将关于使用什么进程位数的决定推迟到操作系统,这将根据自己的喜好 JIT EXE。也就是说,x64 操作系统将创建 64 位进程,x86 操作系统将创建 32 位进程。

Building DLLs as AnyCPU makes them compatible to either process.

将 DLL 构建为 AnyCPU 使它们与任一进程兼容。

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
  • AnyCPU– 加载为 x64 或 x86 程序集,具体取决于调用过程
  • x86– 加载为 x86 程序集;不会从 x64 进程加载
  • x64– 加载为 x64 程序集;不会从 x86 进程加载

回答by Hans Passant

The C# DLL is set up with platform target x86

C# DLL 设置为平台目标 x86

Which is kind of the problem, a DLL doesn't actually get to choose what the bitness of the process will be. 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.

这是一个问题,DLL 实际上并没有选择进程的位数。这完全由 EXE 项目决定,这是第一个加载的程序集,因此其平台目标设置是计算和设置进程位数的程序集。

The DLLs have no choice, they need to be compatible with the process bitness. If they are not then you'll get a big Kaboom with a BadImageFormatException when your code tries to use them.

DLL 没有选择,它们需要与进程位数兼容。如果它们不是,那么当您的代码尝试使用它们时,您将得到一个带有 BadImageFormatException 的大 Kaboom。

So a good selection for the DLLs is AnyCPU so they work either way. That makes lots of sense for C# DLLs, they dowork either way. 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. You canget the build system to generate warnings about that. Which is exactly what you got. Just warnings, it still builds properly.

所以 DLL 的一个很好的选择是 AnyCPU,所以它们可以以任何一种方式工作。这对于 C# DLL 很有意义,它们可以以任何一种方式工作。但可以肯定,不是您的 C++/CLI 混合模式 DLL,它包含非托管代码,只有当进程在 32 位模式下运行时才能正常工作。您可以让构建系统生成关于此的警告。这正是你得到的。只是警告,它仍然可以正常构建。

Just punt the problem. Set the EXE project's Platform target to x86, it isn't going to work with any other setting. And just keep all the DLL projects at AnyCPU.

直接解决问题。将 EXE 项目的平台目标设置为 x86,它不适用于任何其他设置。并将所有 DLL 项目保留在 AnyCPU 中。

回答by yogeshwar gutte

I was getting the same warning i did this:

我得到了同样的警告,我这样做了:

  1. unload project
  2. edit project properties i.e .csproj
  3. add the following tag:

    <PropertyGroup>
        <ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
            None
        </ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
    </PropertyGroup>
    
  4. Reload the project

  1. 卸载项目
  2. 编辑项目属性,即 .csproj
  3. 添加以下标签:

    <PropertyGroup>
        <ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
            None
        </ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
    </PropertyGroup>
    
  4. 重新加载项目

回答by JBourne

I had this problem today and just looking the building configurations in Visual Studio wasn't helping because it showed Any CPU for both the project that wasn't building and the referenced project.

我今天遇到了这个问题,只是在 Visual Studio 中查看构建配置并没有帮助,因为它显示了未构建的项目和引用的项目的任何 CPU。

I then looked in the csproj of the referenced project and found this:

然后我查看了引用项目的csproj,发现了这个:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x64</PlatformTarget>

Somehow this PlatformTarget got added in the middle of a config change and the IDE didn't seem to see it.

不知何故,这个 PlatformTarget 在配置更改的过程中被添加了,IDE 似乎没有看到它。

Removing this line from the referenced project solved my problem.

从引用的项目中删除这一行解决了我的问题。

回答by Dave Cousineau

In addition to David Sacks answer, you may also need to go to the Buildtab of the Project Propertiesand set Platform Targetto x86for the project that is giving you these warnings. Though you might expect it to be, this setting does not seem to be perfectly synchronized with the setting in the configuration manager.

除了大卫·萨克斯的回答,您可能还需要去Build的选项卡Project Properties,并设置Platform Targetx86为是给你这些警告的项目。尽管您可能希望如此,但此设置似乎与配置管理器中的设置并不完全同步。

回答by David W

If your C# DLL has x86-based dependencies, then your DLL itself is going to have to be 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.

如果您的 C# DLL 具有基于 x86 的依赖项,那么您的 DLL 本身将必须是 x86。我真的看不出有什么办法解决这个问题。VS 抱怨将其更改为(例如)x64,因为 64 位可执行文件无法加载 32 位库。

I'm a little confused about the configuration of the C++ project. 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. 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. So I think there are a few gaps on the information side.

我对C++项目的配置有点困惑。为构建提供的警告消息表明它针对 AnyCPU,因为它报告它针对的平台是 [MSIL],但您指出项目的配置实际上是 Win32。本机 Win32 应用程序不应涉及 MSIL - 尽管如果它与 C# 库交互,它可能需要启用 CLR 支持。所以我认为在信息方面存在一些差距。

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.

我能否恭敬地请您查看并发布有关项目的确切配置以及它们之间如何相互关联的更多细节?如果可能的话,很乐意提供进一步的帮助。

回答by Jonathan DeCarlo

For C# projects, the target of x86 does what it sounds like. It says that this assembly only supports x86 architectures. Likewise for x64. Any CPU on the other hand says that I don't care which architecture, I support both. So, the next 2 questions are (1) what is the configuration of the executable that uses these dlls? and (2) what is the bitnessof your OS/Computer? 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. 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. 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. Otherwise, you'll have issues.

对于 C# 项目,x86 的目标就像听起来一样。它说这个程序集只支持 x86 架构。对于 x64 也是如此。另一方面,任何 CPU 都说我不在乎哪种架构,我都支持。那么,接下来的 2 个问题是 (1) 使用这些 dll 的可执行文件的配置是什么?和(2)什么是位数您的操作系统/计算机?我问的原因是因为如果您的可执行文件被编译为在 64 位模式下运行,那么它需要所有依赖项也能够在 64 位模式下运行。您的 Any CPU 程序集应该能够加载,但也许它引用了一些只能在 x86 配置中运行的其他依赖项。如果您计划在 64 位模式下运行可执行文件,请检查所有依赖项和依赖项的依赖项以确保一切都是“任何 CPU”或“x64”。否则,您将遇到问题。

In many ways, Visual Studio does not make compiling a mixture of Any CPU and various architecture dependent assemblies easy. 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.

在许多方面,Visual Studio 并没有使编译 Any CPU 和各种依赖于体系结构的程序集的混合变得容易。这是可行的,但它通常要求必须为 x86 和 x64 分别编译一个本来是“任何 CPU”的程序集,因为某处的某个依赖项有两个版本。

回答by ssamuel

I've had a similar problem before, specifically when adding a test solution to an existing x64 solution, like SharePoint. In my case, it seems to have to do with the fact that certain project templates are added as certain platforms by default.

我以前遇到过类似的问题,特别是在向现有 x64 解决方案(如 SharePoint)添加测试解决方案时。就我而言,这似乎与某些项目模板默认添加为某些平台有关。

Here's the solution that often works for me: set everything to the correct platform in the Configuration Manager (the active configuration drop-down, says Debug normally, is a good way to get to it) and project platform (in project properties), then build, then set everything back to AnyCPU. Sometimes I have to remove and re-add some dependencies (DLLs in each project's Properties) and sometimes the "Run tests in 32 bit or 64 bit process" (double-click Local.testsettings and go to Hosts) has to be changed.

这是通常对我有用的解决方案:在配置管理器中将所有内容设置为正确的平台(活动配置下拉菜单,通常说 Debug,是访问它的好方法)和项目平台(在项目属性中),然后构建,然后将所有内容设置回 AnyCPU。有时我必须删除并重新添加一些依赖项(每个项目属性中的 DLL),有时必须更改“在 32 位或 64 位进程中运行测试”(双击 Local.testsettings 并转到主机)。

It seems to me that this is just setting something then setting it back, but there's probably more going on behind the scenes that I'm not seeing. It's worked fairly consistently for me in the past though.

在我看来,这只是先设置一些东西然后再设置它,但在幕后可能还有更多我没有看到的事情。不过,它在过去对我来说相当一致。