简体   繁体   中英

MSBuild fails to build when compiling C# project referencing C++/CLI DLL in x86

I am using VS2010 and .NET 4.0.

I have two little projects A and B . A is a C++/CLI DLL Project, and B is a C# EXE project referencing A . Both must be compiled in x86, because A uses x86 native dll.

When I build B with VS2010 IDE, B compiles well. Next I try to build B with MSBuild with following command line

MSBuild B.csproj /property:Platform=x86;Configuration=Release

And it fails with following error.

" A .vcxproj" (default target) (16) -> (InvalidPlatformError target) -> C:\\Program Files (x86)\\MSBuild\\Microsoft.Cpp\\v4.0\\Microsoft.Cpp.InvalidPlatform.Targets(23,7): error MSB8007: The Platform for project ' A .vcxproj' is invalid. Platform='x86'. You may be seeing this message because you are trying to build a project without a solution file, and have specified a non-default Platform that doesn't exist for this project. [ A .vcxproj]

It seems to happen because C++/CLI uses "Win32" as platform name, whereas C# uses "x86". So when I specify "x86", It fails to build A . If "Win32", fails to build B .

I have to use MSBuild because of auto building. Default platform for B is fixed to AnyCPU (and I cannot change it), so I cannot use default platform trick and must specify "x86" when building with MSBuild. How can I do this? Is there any way to change the platform name, or, the better way to use MSBuild? Can I do this without using default platform?

For this one I cannot see any way around it except for removing the dependency has on the native x86 dll by replacing it with something which can interface with it and be compiled as "AnyCPU"( not sure what dll/s you're restricted by ). 对本机x86 dll的依赖关系(通过将其替换为可以与之交互并被编译为“ AnyCPU”的东西)(除去不确定的dll / s)之外,我看不到其他任何解决方法通过)。 Basically my understanding is that being win32 means it can run on more processors than a x86 compiled program and so they're not compatible from MSbuild's point of view. What happens if you specify "AnyCPU" for the platform?

If you have a solution for containing both projects and set it up properly, you should be able to build that solution using msbuild (afaik the solution will first build A using Win32 and then B using x86).

Another option is instead of referencing project A from B, you should just add a reference A's output dll. For that to work, first make sure A.dll goes in the same directory as B.exe. Then add a reference to project B by browing to the output directory and selecting A.dll. Also set 'Copy Local' to false since that's not needed then.

just found an interesting approach to this topic: extending the .csproj file with some custom XML in order to have the dependent .vcxproj built correctly.

http://troyparsons.com/blog/2012/08/fixing-csproj-builds-that-reference-vcxproj-files-using-assignprojectconfiguration-task/#comment-843

just in case the link would go dead, here is the important part. don't know why or how it works, but it did the job for me!

<Target Name="PrepProjectConfiguration" BeforeTargets="PrepareForBuild" Condition="'$(Platform)' == 'x86'">
   <AssignProjectConfiguration
         CurrentProjectConfiguration="$(Configuration)"
         CurrentProjectPlatform="$(Platform)"
         ProjectReferences="@(ProjectReference)"
         ResolveConfigurationPlatformUsingMappings="true">
      <Output TaskParameter="AssignedProjects" ItemName="ProjectReferenceWithConfiguration" />
   </AssignProjectConfiguration>
   <ItemGroup>
      <ProjectReference Remove="@(ProjectReferenceWithConfiguration)" />
   </ItemGroup>
   <Message Text="  regular reference %(ProjectReference.Identity)" />
   <Message Text="re-mapped reference %(ProjectReferenceWithConfiguration.Identity) - %(ProjectReferenceWithConfiguration.Configuration)|%(ProjectReferenceWithConfiguration.Platform)" />
</Target>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM