我是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,这是唯一的其他选项。
我哪里做错了?
这是一个非常顽固的警告,虽然这是一个有效的警告,但在某些情况下,由于使用第三方组件和其他原因,它无法解决。我有一个类似的问题,除了警告是因为我的项目平台是AnyCPU,我引用了一个为AMD64构建的MS库。顺便说一下,这是在Visual Studio 2010中,并且似乎是通过安装VS2012和。net 4.5引入的。
因为我不能更改我引用的MS库,而且我知道我的目标部署环境永远只能是64位的,所以我可以安全地忽略这个问题。
那警告呢?微软在回应一份Connect报告时表示,一种选择是禁用该警告。只有在非常了解自己的解决方案体系结构、完全理解部署目标并知道在开发环境之外这不是真正的问题时,您才应该这样做。
您可以编辑您的项目文件并添加此属性组和设置来禁用警告:
<PropertyGroup>
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
</PropertyGroup>
这个警告似乎已经在新的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.
转到“构建|配置管理器”菜单项。
在列表中找到你的项目,在Platform下面会显示Any CPU
从下拉菜单中选择“Any CPU”选项,然后选择<New..>
从对话框中,从“New Platform”下拉菜单中选择x86,并确保在“Copy settings From”下拉菜单中选择“Any CPU”。
点击确定
调试和发布配置都要选择x86。
这将使警告消失,并声明您的程序集或项目现在不再“任何CPU”兼容,而是特定于x86。如果您正在构建一个具有x64依赖关系的64位项目,这也适用;您只需选择x64即可。
另外需要注意的是,如果项目是纯。net项目,那么它们通常可以与“任何CPU”兼容。只有当你引入一个针对特定处理器架构的依赖项(第三方dll或你自己的c++托管项目)时,这个问题才会出现。
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 bitness of 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.
在许多方面,Visual Studio并没有使编译任意CPU和各种依赖于体系结构的程序集的混合变得容易。这是可行的,但它通常要求必须针对x86和x64分别编译“任何CPU”的程序集,因为某个依赖的某个依赖有两个版本。