Building Visual Studio project with different platforms via MSBuild

南楼画角 提交于 2019-12-10 08:07:19

问题


I have 3 projects with configuration:

  • Project A: Debug|AnyCPU, Release|AnyCPU
  • Project B: Debug|AnyCPU, Release|AnyCPU
  • Project C: Debug|x86, Debug|x64, Release|x86, Release|x64

Project C have B in dependencies and B have A in dependencies. (A <- B <- C)

I use *.bat file to build its from command line:

msbuild A.csproj /target:Build /property:Configuration=Debug;Platform=AnyCPU /verbosity:minimal
msbuild A.csproj /target:Build /property:Configuration=Release;Platform=AnyCPU /verbosity:minimal<br/>
msbuild B.csproj /target:Build /property:Configuration=Debug;Platform=AnyCPU /verbosity:minimal
msbuild B.csproj /target:Build /property:Configuration=Release;Platform=AnyCPU /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Debug;Platform=x86 /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Release;Platform=x86 /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Debug;Platform=x64 /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Release;Platform=x64 /verbosity:minimal

And recieve error:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(609,5): error : The OutputPath property is not set for project 'A.csproj'. Please check to make sure that you have specified a valid combination of Configuration and Platform for this project. Configuration='Debug' 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 Configuration or Platform that doesn't exist for this project. [A.csproj] C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(609,5): error : The OutputPath property is not set for project 'B.csproj'. Please check to make sure that you have specified a valid combination of Configuration and Platform for this project. Configuration='Debug' 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 Configuration or Platform that doesn't exist for this project. [B.csproj]


回答1:


I found the solution for my problem. Use Choose element in *.csproj file for detect building via Visual Studio or via MSBuild and use Reference (instead of ProjectReference) for MSBuild.

<Choose>
  <When Condition="'$(BuildingInsideVisualStudio)' == 'true'">
    <ItemGroup>
      <ProjectReference Include="A.csproj">
        <Project>{AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA}</Project>
        <Name>A</Name>
        <Private>True</Private>
      </ProjectReference>
      <ProjectReference Include="B.csproj">
        <Project>{BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB}</Project>
        <Name>B</Name>
        <Private>True</Private>
      </ProjectReference>
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <Reference Include="A, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx, processorArchitecture=MSIL">
        <HintPath>A.dll</HintPath>
        <Private>True</Private>
      </Reference>
      <Reference Include="B, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx, processorArchitecture=MSIL">
        <HintPath>B.dll</HintPath>
        <Private>True</Private>
      </Reference>
    </ItemGroup>
  </Otherwise>
</Choose>



回答2:


You can make this work if you define a custom build configurations in your solution file.

Open the solution in Visual Studio, then open Configuration Manager for the solution. In the Active solution platforms drop-down there will be <New...> option that would allow you to create x86 and x64 platforms for the solution. After you've created them select each one in that same drop-down and make sure that in the list you have Any CPU for your projects A and B and that the project C has correspondingly x86 or x64 selected. Also make sure that Build checkbox is checked.

Now switch the Active solution configuration to Release and define those extra platforms the same way.

After you do that you'll be able to build all 3 projects specifying just the solution file in the MSbuild command line:

msbuild ABC.sln /target:Build /property:Configuration=Debug;Platform=x86 /verbosity:minimal
msbuild ABC.sln /target:Build /property:Configuration=Release;Platform=x86 /verbosity:minimal
msbuild ABC.sln /target:Build /property:Configuration=Debug;Platform=x64 /verbosity:minimal
msbuild ABC.sln /target:Build /property:Configuration=Release;Platform=x64 /verbosity:minimal



回答3:


You can try to build project with devenv.exe from command line. I think that this will not have problems you experienced but i remember something about this build option to be deprecated and it sometimes hanged for no reason. I wrote this as one option, just because other two are not good for me either.



来源:https://stackoverflow.com/questions/12348645/building-visual-studio-project-with-different-platforms-via-msbuild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!