2
votes

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?

3
The platform name for a C# project is meaningless, you can name it anything you like. Only the Platform Target setting matters. If you cannot change a meaningless name then that's where it kinda ends. But you really do have to explain why building project B also gets project A built. That can only happen when you build a solution, not when you build a project.Hans Passant
@HansPassant Because project B references project A. That information is included in .csproj file, so MSBuild tries to build A first. And I really cannot find where I can change platform name for a C# project.. except for editing .csproj file directly, which might have some potential problems or side effects.Unused

3 Answers

0
votes

For this one I cannot see any way around it except for removing the dependency A 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 ). 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?

0
votes

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.

0
votes

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>