我是Visual Studio 2010项目配置的新手,但我做了一些研究,仍然不能完全解决这个问题。我有一个Visual Studio解决方案的c++ DLL引用c# DLL。c# DLL引用了一些其他DLL,一些在我的项目内,一些在外部。当我试图编译c++ DLL时,我得到这个警告:

警告MSB3270:正在构建的“MSIL”项目的处理器架构与参考“[内部c# dll]”,“x86”的处理器架构之间不匹配。

它告诉我去配置管理器调整我的架构。c# DLL是用目标平台x86建立的。如果我尝试将其更改为其他东西,如任何CPU,它会抱怨,因为它所依赖的外部dll之一具有平台目标x86。

当我查看配置管理器时,它显示了我的c# DLL作为x86和我的c++项目作为Win32的平台。这似乎是正确的设置;当然,我不希望我的c++项目的项目平台设置为x64,这是唯一的其他选项。

我哪里做错了?


当前回答

对于我的项目,我要求能够构建到x86和x64。这样做的问题是,每当您在使用一个引用时添加引用,那么当您构建另一个引用时它就会报错。

我的解决方案是手动编辑*。Csproj文件,这样行:

<Reference Include="MyLibrary.MyNamespace, Version=1.0.0.0, Culture=neutral, processorArchitecture=x86"/>

<Reference Include="MyLibrary.MyNamespace, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64"/>

<Reference Include="MyLibrary.MyNamespace, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"/>

改成这样:

<Reference Include="MyLibrary.MyNamespace, Version=1.0.0.0, Culture=neutral"/>

其他回答

这是一个非常顽固的警告,虽然这是一个有效的警告,但在某些情况下,由于使用第三方组件和其他原因,它无法解决。我有一个类似的问题,除了警告是因为我的项目平台是AnyCPU,我引用了一个为AMD64构建的MS库。顺便说一下,这是在Visual Studio 2010中,并且似乎是通过安装VS2012和。net 4.5引入的。

因为我不能更改我引用的MS库,而且我知道我的目标部署环境永远只能是64位的,所以我可以安全地忽略这个问题。

那警告呢?微软在回应一份Connect报告时表示,一种选择是禁用该警告。只有在非常了解自己的解决方案体系结构、完全理解部署目标并知道在开发环境之外这不是真正的问题时,您才应该这样做。

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

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

There should be a way to make a .NET EXE/DLL AnyCPU, and any unmanaged DLLs it depends on compiled both with x86 and x64, both bundled perhaps with different filenames and then the .NET module dynamically loading the correct one based on its runtime processor architecture. That would make AnyCPU powerful. If the C++ DLL only supports x86 or x64 then AnyCPU is of course pointless. But the bundling both idea I have yet to see implemented as the configuration manager does not even provide a means to build the same project twice with a different configuration/platform for multiple bundling allowing AnyCPU or even other concepts like any configuration to be possible.

使用https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build # directorybuildprops-example:

在解决方案文件夹中添加一个Directory.Build.props文件 把这个粘贴进去:

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

我解决了这个警告,将“配置管理器”更改为发布(混合平台)。

我以前遇到过类似的问题,特别是在向现有的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.

在我看来,这只是设定了一些事情,然后又把它放回去,但在幕后可能有更多我没有看到的事情发生。不过在过去,这对我来说是相当稳定的。