Obtaining MSBuild to Compile a Project with Dependencies for Win32 and Win64 Platforms
I have a large solution with over 10 projects in it in C ++.
The whole solution is x64 only except for project P , which requires both x64 and win32 versions (the corresponding one is loaded at runtime).
The P project depends on several other projects for lib files: C and H , which are compiled into libraries.
P has a reference to C and H as follows:
<ProjectReference Include="..\C\C.vcxproj">
<Project>{....}</Project>
</ProjectReference>
<ProjectReference Include="..\H\H.vcxproj">
<Project>{....}</Project>
</ProjectReference>
I want to create a P project for both platforms.
I decided to do it from a meta-P project that calls P :
<MSBuild Projects="..\P\P.vcxproj" Properties="Platform=Win32"/>
<MSBuild Projects="..\P\P.vcxproj" Properties="Platform=x64"/>
This allows developers to freely modify P , and then both versions are created at once, creating a meta-P .
The problem is that when meta-P calls MSBuild in project P, references to C and H depend on the Solution environment (in which the active platform is always x64).
When it comes to linking Win32 P to its correct C.lib and H.lib , the open solution configuration opens and studio tries to link it to the x64 version and fails.
I temporarily solved it using the exec task in Meta P- , MsBuild.exe to run directly on the P . This ignored the properties of the Visual Studio environment.
What was the correct solution to read the platform correctly?
source to share
The correct solution is to add the undocumented ShouldUnsetParentConfigurationAndPlatform property and set it to false .
The only correct place to put it is inside each Project Reference in Project P , otherwise it is ignored.
So it looks like this:
<ProjectReference Include="..\C\C.vcxproj">
<Project>{....}</Project>
<Properties>ShouldUnsetParentConfigurationAndPlatform=false</Properties>
</ProjectReference>
<ProjectReference Include="..\H\H.vcxproj">
<Project>{....}</Project>
<Properties>ShouldUnsetParentConfigurationAndPlatform=false</Properties>
</ProjectReference>
This forces Visual Studio to follow with C and H inherited properties instead of reading from the solution environment.
Edit: See the helpful link from the comment: https://github.com/Microsoft/msbuild/blob/98d38cb/src/XMakeTasks/AssignProjectConfiguration.cs#L198-L218
source to share
I'm not sure if you need Meta-P. At work, we had a similar problem.
In P, we added an ItemGroup with two vcxprojs:
<CppProjects Include="...\P64.vcxproj">
<Properties>Platform=x64</Properties>
</CppProjects>
<CppProjects Include="...\P32.vcxproj">
<Properties>Platform=Win32</Properties>
</CppProjects>
Platform metadata will invoke the expected platform.
Then add the build dependency to P.csproj with:
<Target Name="BuildCpp" BeforeTargets="Build">
<MSBuild Projects="@(CppProjects)" Properties="Configuration=$(Configuration)" RemoveProperties="Platform" />
</Target>
source to share