Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何同时为x86和x64配置VS2012解决方案_C#_Visual Studio_Msbuild - Fatal编程技术网

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

C# 如何同时为x86和x64配置VS2012解决方案,c#,visual-studio,msbuild,C#,Visual Studio,Msbuild,我们有一个VS2012.NET 4产品,它有两个不同的SKU a和B,我们目前只为x86构建。 我们还有常见的配置Debug和Release,这意味着我们目前有4种配置 德布加 调试 释放 释放b 查看其中一个.csproj文件,它看起来像这样 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugA|x86' "> <OutputPath>..\bin\DebugA\</O

我们有一个VS2012.NET 4产品,它有两个不同的SKU a和B,我们目前只为x86构建。 我们还有常见的配置
Debug
Release
,这意味着我们目前有4种配置

  • 德布加
  • 调试
  • 释放
  • 释放b
查看其中一个.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>

..\bin\DebugA\
..\bin\ReleaseA\
..\bin\DebugB\
..\bin\ReleaseB\
显然,添加x64将使4到8种不同的组合增加一倍,而VS似乎还可以随时添加任何CPU平台配置。确保在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> 

..\bin\${Platform}\${Configuration}\
..\bin\${Platform}\${Configuration}\
从理论上讲,这应该给我所需要的8种不同的组合,只有两个区块。但是现在查看VS,我看不到neitehr x86或x64是该项目可用的构建平台。看起来VS实际存储构建平台的唯一方法是将它们编码为PropertyGroup上的异常条件?说不是这样

难道没有办法制作一个“好看”的多平台.csproj,它可以很好地与VS配合使用吗

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

编辑:


queston似乎有点让人困惑:说清楚了,我想知道当有许多项目和配置平台的八种组合时,如何设置、维护和概述项目的配置,以及我的解决方案的构建配置。我知道如何手动执行此操作,但在200多个属性页中的某一页上,我会发疯或出错。

您想要批量生成,可以在其中选择要运行的不同生成


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

在我看来,在配置名称中组合平台和SKU是不明智的。在您的情况下,我建议只使用调试和发布项目配置。您的解决方案应该有一个针对SKU A的项目和一个针对SKU B的单独项目(共享任何公共文件)。除了两种构建配置外,每个项目还可以针对x86和x64平台。然后,您可以添加任意数量的解决方案配置,而无需使单个项目配置变得更加复杂。

如果您不介意手动更改项目文件一次,则可以将所有共享配置放在一个配置文件中,并引用每个项目的配置文件。为此,您需要首先创建配置文件。此文件只是一个普通的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>
<?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>
要链接配置文件,您需要:

  • 从第一个
    属性组中删除配置文件中已定义的属性
  • 添加行
    $(MSBuildProjectDirectory)\..
    。如果仅从VisualStudio生成(因为VisualStudio自动定义
    SolutionDir
    变量),则不需要这样做,但如果还希望通过MsBuild生成项目,则需要这样做。这一行还假设每个项目都在它自己的子目录中,并且解决方案文件是每个项目文件上的一个目录,即您的结构类似于:

    source
        MyProject
            MyProject.csproj
        MySolution.sln 
    
    <?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>
    
  • 在第一个
    属性组的下方添加以下行
    。这向MsBuild(以及Visual Studio)指示您要导入配置文件

  • 删除生成配置
  • 在文件末尾删除行
    。这在配置文件中定义,因此不再需要
所有这些之后,您的项目文件应该如下所示:

source
    MyProject
        MyProject.csproj
    MySolution.sln 
<?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>

$(MSBuildProjectDirectory)\。。
{33017F71-5A1C-4113-9041-4DD3F58921D0}
图书馆
我的项目
我的项目
注:

  • 如果您采用这种方法,VisualStudio应该识别所有不同的构建配置,并允许您选择正确的配置。请注意,您可能需要进入解决方案的“配置管理器”,以便从特定解决方案配置中包括或排除项目
  • 如果采用这种方法,则无法再通过项目的属性页更改任何全局定义的属性。您必须在配置文件中进行更改,然后这些更改将反映在每个项目的属性中
  • 如果您使用的是Visual Studio 2010或更早版本,那么如果您对配置文件进行了更改,则需要重新加载解决方案(如果已打开),因为Visual Studio 2010不会检测到包含文件的更改。Visual Studio 2012应该能够检测到包含文件的更改
该功能不只是用于运行多个(不同)版本吗?我的问题是如何建立和维护一个大型的构建矩阵,而不必做重复和容易出错的工作。批量生成是否有帮助