gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:36:48 25 4
gpt4 key购买 nike

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

  • 调试A
  • 调试B
  • 版本A
  • 版本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>

显然,添加 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 无法在各个项目的属性窗口中显示任何平台吗?

编辑:

问题似乎有点困惑:要清楚,我想知道如何设置,维护和概述项目的配置,以及我的解决方案的构建配置,当有很多项目和八种组合时配置|平台。我知道如何手动执行此操作,但并非没有失去理智或在 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您可以使用像 $(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中删除那些已经在你的配置文件中定义的
  • 添加行 <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 应该可以识别所有不同的构建配置,并允许您选择正确的配置。请注意,您可能需要进入解决方案的“配置管理器”,以便从特定解决方案配置中包含或排除项目。
  • 如果您采用此方法,则无法再通过项目的属性页更改任何全局定义的属性。您必须在配置文件中进行更改,然后这些更改将反射(reflect)在每个项目的属性中。
  • 如果您使用的是 Visual Studio 2010 或更早版本,那么如果您对配置文件进行了更改,则需要重新加载解决方案(如果您已打开),因为 Visual Studio 2010 不会检测包含文件的更改。 Visual Studio 2012 应该能够检测到包含文件的更改。

关于c# - 如何同时为 x86 和 x64 配置 VS2012 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15921006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com