Vin
Vin

Reputation: 6145

How to add and compile for custom 'Platform' switch for visual studio projects?

We are looking to provide two custom Platform switches (the platform dropdown in the configuration manager) for our projects in 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.

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

<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

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.

Upvotes: 2

Views: 9291

Answers (5)

Gordon
Gordon

Reputation: 96

I've recently asked the same question and it appears that with Visual Studio 2010 you can define your own project platforms. (See Link). Unfortunately, we are still using 2008.

Upvotes: 1

GGirard
GGirard

Reputation: 1185

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?

Upvotes: 0

aster.x
aster.x

Reputation: 81

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.

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.

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.

Upvotes: 7

Scott Dorman
Scott Dorman

Reputation: 42516

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

Upvotes: 1

Joel Lucsy
Joel Lucsy

Reputation: 8696

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.

Upvotes: -1

Related Questions