代码之家  ›  专栏  ›  技术社区  ›  Vin

如何为visual studio项目添加和编译自定义“平台”开关?

  •  1
  • Vin  · 技术社区  · 16 年前

    我们希望提供两个 自定义平台交换机 (修订) .

    例如,一个用于“桌面”,另一个用于“Web”。然后,目标构建任务基于平台切换以自定义方式编译代码。我们不想添加到Debug Release开关中,因为对于每个桌面和Web平台,我们都需要这些开关。

    我们找到了一种尝试的方法,就是修改.csproj文件以添加类似的内容

    <Platform Condition=" '$(Platform)' == '' ">Desktop</Platform>
    

    并添加属性组,如,

        <PropertyGroup Condition=" '$(Platform)' == 'Web' ">
            <DefineConstants>/define Web</DefineConstants>
            <PlatformTarget>Web</PlatformTarget>
          </PropertyGroup>
          <PropertyGroup Condition=" '$(Platform)' == 'Desktop' ">
            <DefineConstants>/define Desktop</DefineConstants>
            <PlatformTarget>Desktop</PlatformTarget>
          </PropertyGroup>
    

    /platform的“桌面”选项无效;必须是任何CPU、x86、安腾或x64

    有人能做到这一点吗?任何指示都会有帮助。

    更新:使用DebugDesktop和ReleaseDesktop会让用户感到更加复杂。因为“桌面”和“网络”实际上是平台,而且还可以在下拉列表中添加新平台(即),所以我认为“平台”开关应该用于完全相同的目的。

    5 回复  |  直到 13 年前
        1
  •  7
  •   aster.x    13 年前

    也许三年后这个话题对某人来说会很有趣。我在配置构建平台时遇到了类似的困难,并解决了它们。

    抛出您给出的错误是因为PlatformTarget属性是用Desctop设置的,而不是因为Platform属性。这两个属性的含义稍有不同。第一个最终指导构建过程的所有参与者应该使用哪个过程体系结构,而第二个允许在IDE中定制构建环境。

    在Visual Studio中创建项目时,默认情况下,ProcessTarget属性可以在具有如下条件限制的PropertyGroups下使用AnyCPU设置 “…|$(平台)”=“…| AnyCPU”

    考虑到上述情况,您的样本可能如下所示:

    <PropertyGroup Condition=" '$(Platform)' == 'Web' ">
        <DefineConstants>Web</DefineConstants>
        <PlatformTarget>AnyCPU</PlatformTarget>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Platform)' == 'Desktop' ">
        <DefineConstants>Desktop</DefineConstants>
        <PlatformTarget>AnyCPU</PlatformTarget>
    </PropertyGroup>
    

    希望对你有用。

        2
  •  1
  •   Scott Dorman    16 年前

    您应该能够使用Configuration Manager对话框创建新平台。

        3
  •  1
  •   Gordon    14 年前
        4
  •  0
  •   GGirard    9 年前

    我也在找这个。。。但是根据 https://msdn.microsoft.com/en-us/library/bb629394.aspx ,MSBuild引擎的唯一有效值是“任意CPU”、“x86”和“x64”。

        5
  •  -1
  •   Joel Lucsy    16 年前

    将“Debug”和“Release”重命名为“Debug Desktop”和“Release Desktop”,并将它们复制为“Debug Web”和“Release Web”。然后以这种方式设置常量。