如何为visual studio项目添加和编译自定义'Platform'开关?

时间:2022-09-01 08:03:17

We are looking to provide two custom Platform switches (the platform dropdown in the configuration manager) for our projects in Visual Studio.

我们希望在Visual Studio中为我们的项目提供两个自定义平台开关(配置管理器中的平台下拉列表)。

For example one for 'Desktop' and one for 'Web'. The target build tasks then compile the code in a custom way based on the platform switch. We don't want to add to the Debug Release switch because we would need those for each Desktop and Web platforms.

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

We found one way to attempt this, is to modify the .csproj file to add something like this

我们发现尝试这种方法的一种方法是修改.csproj文件以添加这样的东西

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

and add propertygroups like,

并添加属性组,如,

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

But still this doesn't work, and compiler throws an error

但仍然无法正常工作,编译器会抛出错误

Invalid option 'Desktop' for /platform; must be anycpu, x86, Itanium or x64

/ platform的选项'Desktop'无效;必须是anycpu,x86,Itanium或x64

So does it have to be one of those options and can't we add our custom platforms?

那么它必须是其中一个选项,我们不能添加我们的自定义平台吗?

Has anyone been able to do this? any pointers would be helpful.

有没有人能够这样做?任何指针都会有所帮助。

Update: Using DebugDesktop and ReleaseDesktop will make it more complicated for users. Because 'desktop' and 'web' are actually platforms and also there is ability to add new platforms in the dropdown (i.e. ), I believe 'platform' switch should be used for the exact same purpose.

更新:使用DebugDesktop和ReleaseDesktop会使用户更加复杂。因为“桌面”和“网络”实际上是平台,并且还有能力在下拉列表中添加新平台(即),我相信“平台”切换应该用于完全相同的目的。

5 个解决方案

#1


2  

You should be able to use the Configuration Manager dialog to create new platforms.

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

#2


4  

May be this subject will be interesting for somebody after three years. I had similar difficulties with configuring build platforms and resolved them.

可能这个主题对于三年后的某个人来说会很有趣。我在配置构建平台和解决它们方面遇到了类似的困难。

The error you gave was thrown because the PlatformTarget property was set with Desctop, not because the Platform property. These two properties have a little bit different meaning. The first one really eventually instructs all participants of build process which process architecture should be utilized, while the second one allows customize build circumstances inside IDE.

抛出的错误是因为PlatformTarget属性是使用Desctop设置的,而不是因为Platform属性。这两个属性有一些不同的含义。第一个真正最终指示构建过程的所有参与者应该使用哪个过程架构,而第二个允许在IDE内部定制构建环境。

When project is created in the Visual Studio, the ProcessTarget property may be set by default with AnyCPU under PropertyGroups that have conditional restrictions like this "'...|$(Platform)' == '...|AnyCPU'". But it doesn't enforce you to do the same. The ProcessTarget property can easily be set with AnyCPU for Platform property having other values.

在Visual Studio中创建项目时,ProcessTarget属性可以默认设置为PropertyGroups下具有条件限制的AnyCPU,例如“'... | $(Platform)'=='... | AnyCPU'”。但它并没有强制你做同样的事情。可以使用具有其他值的AnyCPU for Platform属性轻松设置ProcessTarget属性。

Considering described above, your sample may looks like this:

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

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

it must be working.

它必须工作。

Hope it's useful for you.

希望它对你有用。

#3


1  

I've recently asked the same question and it appears that with Visual Studio 2010 you can define your own project platforms. (See http://blogs.msdn.com/b/vcblog/archive/2008/11/20/printf-hello-msbuild-n.aspx). Unfortunately, we are still using 2008.

我最近问了同样的问题,看来在Visual Studio 2010中你可以定义自己的项目平台。 (参见http://blogs.msdn.com/b/vcblog/archive/2008/11/20/printf-hello-msbuild-n.aspx)。不幸的是,我们仍在使用2008。

#4


0  

Looking for this too... But according to https://msdn.microsoft.com/en-us/library/bb629394.aspx, the only valid values for MSBuild engine are "Any CPU", "x86", and "x64". Is this customization really supported?

也在寻找这个...但是根据https://msdn.microsoft.com/en-us/library/bb629394.aspx,MSBuild引擎的唯一有效值是“Any CPU”,“x86”和“x64” ”。这种定制是否真的受到支持?

#5


-1  

Rename "Debug" and "Release" to "Debug Desktop" and "Release Desktop" and copy them into names "Debug Web" and "Release Web". Then set your constants up that way.

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

#1


2  

You should be able to use the Configuration Manager dialog to create new platforms.

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

#2


4  

May be this subject will be interesting for somebody after three years. I had similar difficulties with configuring build platforms and resolved them.

可能这个主题对于三年后的某个人来说会很有趣。我在配置构建平台和解决它们方面遇到了类似的困难。

The error you gave was thrown because the PlatformTarget property was set with Desctop, not because the Platform property. These two properties have a little bit different meaning. The first one really eventually instructs all participants of build process which process architecture should be utilized, while the second one allows customize build circumstances inside IDE.

抛出的错误是因为PlatformTarget属性是使用Desctop设置的,而不是因为Platform属性。这两个属性有一些不同的含义。第一个真正最终指示构建过程的所有参与者应该使用哪个过程架构,而第二个允许在IDE内部定制构建环境。

When project is created in the Visual Studio, the ProcessTarget property may be set by default with AnyCPU under PropertyGroups that have conditional restrictions like this "'...|$(Platform)' == '...|AnyCPU'". But it doesn't enforce you to do the same. The ProcessTarget property can easily be set with AnyCPU for Platform property having other values.

在Visual Studio中创建项目时,ProcessTarget属性可以默认设置为PropertyGroups下具有条件限制的AnyCPU,例如“'... | $(Platform)'=='... | AnyCPU'”。但它并没有强制你做同样的事情。可以使用具有其他值的AnyCPU for Platform属性轻松设置ProcessTarget属性。

Considering described above, your sample may looks like this:

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

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

it must be working.

它必须工作。

Hope it's useful for you.

希望它对你有用。

#3


1  

I've recently asked the same question and it appears that with Visual Studio 2010 you can define your own project platforms. (See http://blogs.msdn.com/b/vcblog/archive/2008/11/20/printf-hello-msbuild-n.aspx). Unfortunately, we are still using 2008.

我最近问了同样的问题,看来在Visual Studio 2010中你可以定义自己的项目平台。 (参见http://blogs.msdn.com/b/vcblog/archive/2008/11/20/printf-hello-msbuild-n.aspx)。不幸的是,我们仍在使用2008。

#4


0  

Looking for this too... But according to https://msdn.microsoft.com/en-us/library/bb629394.aspx, the only valid values for MSBuild engine are "Any CPU", "x86", and "x64". Is this customization really supported?

也在寻找这个...但是根据https://msdn.microsoft.com/en-us/library/bb629394.aspx,MSBuild引擎的唯一有效值是“Any CPU”,“x86”和“x64” ”。这种定制是否真的受到支持?

#5


-1  

Rename "Debug" and "Release" to "Debug Desktop" and "Release Desktop" and copy them into names "Debug Web" and "Release Web". Then set your constants up that way.

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