我是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,这是唯一的其他选项。

我哪里做错了?


当前回答

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

然后我在参考项目的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>

不知怎么的,这个PlatformTarget是在配置更改的过程中添加的,而IDE似乎没有看到它。

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

其他回答

除了David Sacks的回答,你可能还需要去项目属性的Build选项卡,为给你这些警告的项目设置平台目标为x86。尽管您可能希望如此,但此设置似乎与配置管理器中的设置并不完全同步。

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

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

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

如果你的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.

我能否恭敬地请您回顾并发布更多关于这些项目的确切配置以及它们是如何相互关联的细节?如有可能,我很乐意进一步提供帮助。

这个警告似乎已经在新的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++托管项目)时,这个问题才会出现。

只是想为那些在这里找不到答案的人解决他们的问题。

运行应用程序时,确保正确设置了解决方案平台下拉菜单。我的是在x86上,这反过来又导致了这个问题。