繁体   English   中英

如何同时为x86和x64配置VS2012解决方案

[英]How to configure a VS2012 solution for x86 and x64 simultaneously

我们有一个VS2012 .NET 4产品,它有两个不同的SKU A和B,我们目前仅为x86构建。 我们也有通常的Configurations DebugRelease意味着我们目前有4种配置。

  • DEBUGA
  • DebugB
  • ReleaseA
  • ReleaseB

查看其中一个.csproj文件,它看起来像这样

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugA|x86' ">
    <OutputPath>..\bin\DebugA\</OutputPath>
</PropertyGroup> 
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseA|x86' ">
    <OutputPath>..\bin\ReleaseA\</OutputPath>
</PropertyGroup> 
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugB|x86' ">
    <OutputPath>..\bin\DebugB\</OutputPath>
</PropertyGroup> 
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseB|x86' ">
    <OutputPath>..\bin\ReleaseB\</OutputPath>
</PropertyGroup>

显然,添加x64会使这个从4到8种不同的组合加倍,VS似乎也会在任何时候添加AnyCPU平台配置。 确保在30多个项目中正确配置所有8个项目需要在VS中进行大量点击,并且很容易出错。

我已经阅读了其他一些SO问题,其中解决了多目标问题,以及在参考路径中使用$ {Platform}涉及的不同平台包含不同参考的建议之一。 我想我可以为我的项目配置做类似的事情,所以我在尝试做多平台时尝试了这个:

<PropertyGroup Condition=" '$(Configuration)' == 'DebugA' Or '$(Configuration)' == 'DebugB' ">
     <OutputPath>..\bin\${Platform}\${Configuration}\</OutputPath>
</PropertyGroup> 
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseA' Or '$(Configuration)' == 'ReleaseB' ">
     <OutputPath>..\bin\${Platform}\${Configuration}\</OutputPath>
</PropertyGroup> 

从理论上讲,这应该只需要两个块就可以满足我对所有8种不同组合的需求。 但是现在看看VS我看不到neitehr x86和x64作为该项目的可用构建平台。 看起来VS实际存储构建平台的唯一方法是将它们编码为属性组中的怪异条件? 说它不是这样的......

有没有办法制作一个“漂亮”的多平台.csproj,它可以很好地与VS一起使用?

我可以创建.csprojs,然后决定不在VS中编辑它们,相信msbuild将使用正确的平台,即使VS无法在各个项目的属性窗口中显示任何平台吗?

编辑:

看起来问题有点令人困惑:要清楚,我想知道如何设置,维护和概述项目的配置,以及我的解决方案的构建配置,当有很多项目和8个组合的config | platform时。 我知道如何手动完成此操作,但不是没有失去理智或在200多个属性页面中出错。

如果您不介意必须手动更改项目文件,那么您可以将所有共享配置放在配置文件中并从每个项目中引用它。 为此,您需要先创建配置文件。 此文件只是一个普通的MsBuild文件,其中包含您要在所有项目之间共享的所有信息(如构建配置)。 该文件看起来大致如下:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5"
         DefaultTargets="Build"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <!-- VS information -->
        <ProductVersion>9.0.30729</ProductVersion>
        <SchemaVersion>2.0</SchemaVersion>

        <!-- Default configuration -->
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>

        <!-- Project directories -->
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <OutputPath>$(SolutionDir)\..\build\bin\$(Platform)\$(Configuration)\</OutputPath>
        <IntermediateOutputPath>$(SolutionDir)\..\build\temp\bin\obj\$(AssemblyName)\$(Platform)\$(Configuration)\</IntermediateOutputPath>

        <!-- Build configuration -->
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <DefineConstants>TRACE;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <DefineConstants>TRACE;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <DefineConstants>TRACE;CODE_ANALYSIS</DefineConstants>
    </PropertyGroup>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
  • 第一个PropertyGroup定义整体常量,即所有项目用于所有构建配置的常量。 正如您在OutputPath看到的OutputPath您可以使用构建变量(如$(Platform)$(Configuration)来确定二进制文件的位置等。
  • 第一个PropertyGroup还有一堆通常由visual studio定义的设置,例如ProductVersion 从技术上讲,您不必移动它们,但移动它们确实可以减少项目文件中的混乱,如果您关心它。
  • 以下部分定义了不同构建配置的不同设置。

一旦定义了配置文件,比如称为BaseConfigurations.targets ,就必须编辑项目文件。 不幸的是,您将不得不浏览所有项目文件,但您只需要执行一次。 链接配置文件后,您可以通过更改配置文件来更改所有共享配置。

一个普通的项目文件看起来大致如下:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{33017F71-5A1C-4113-9041-4DD3F58921D0}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>MyProject</RootNamespace>
    <AssemblyName>MyProject</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <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>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

要链接配置文件,您需要:

  • 从第一个PropertyGroup删除已在配置文件中定义的PropertyGroup
  • 添加行<SolutionDir Condition="'$(SolutionDir)' == '' or '$(SolutionDir)' == '*undefined*'">$(MSBuildProjectDirectory)\\..</SolutionDir> 如果您只从Visual Studio构建(因为Visual Studio自动定义SolutionDir变量),则不需要这样做,但如果您还想通过MsBuild构建项目,则必须这样做。 这一行还假设每个项目都在它自己的子目录中,并且解决方案文件是每个项目文件中的一个目录,即您的结构类似于:

     source MyProject MyProject.csproj MySolution.sln 
  • 在第一个PropertyGroup下方添加以下行<Import Project="$(SolutionDir)\\BaseConfiguration.targets" /> 这向MsBuild(以及Visual Studio)指示您要导入配置文件。

  • 删除构建配置
  • 在文件的末尾删除行<Import Project="$(MSBuildToolsPath)\\Microsoft.CSharp.targets" /> 这在您的配置文件中定义,因此不再需要。

毕竟,您的项目文件应该类似于:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <SolutionDir Condition="'$(SolutionDir)' == '' or '$(SolutionDir)' == '*undefined*'">$(MSBuildProjectDirectory)\..</SolutionDir>
    <ProjectGuid>{33017F71-5A1C-4113-9041-4DD3F58921D0}</ProjectGuid>
    <OutputType>Library</OutputType>
    <RootNamespace>MyProject</RootNamespace>
    <AssemblyName>MyProject</AssemblyName>
  </PropertyGroup>
  <Import Project="$(SolutionDir)\BaseConfiguration.targets" />
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

笔记:

  • 如果您遵循此方法,Visual Studio应识别所有不同的构建配置,并允许您选择正确的构建配置。 请注意,您可能需要进入解决方案的“配置管理器”,以便在特定解决方案配置中包含或排除项目。
  • 如果您遵循此方法,则无法再通过项目的属性页更改任何全局定义的属性。 您必须在配置文件中进行更改,然后这些更改将反映在每个项目的属性中。
  • 如果您使用的是Visual Studio 2010或更早版本,那么如果对配置文件进行更改,则需要重新加载解决方案(如果已打开),因为Visual Studio 2010未检测到包含文件的更改。 Visual Studio 2012应该能够检测包含文件的更改。

您需要批量构建,您可以在其中选择要运行的不同构建。

在此输入图像描述

您可以使用工具 - >选项 - >键盘将其映射到键盘快捷键,然后搜索Build.BatchBuild

在我看来,在您的配置名称中组合平台和SKU是不明智的。 在您的情况下,我建议仅坚持使用Debug和Release项目配置。 您的解决方案应该有一个SKU A项目和一个SKU B的单独项目(共享任何常见文件)。 除了两个构建配置之外,每个项目都可以针对x86和x64平台。 然后,您可以添加尽可能多的解决方案配置,而无需使单个项目配置更复杂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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