How do I specify the platform for MSBuild?

Msbuild

Msbuild Problem Overview


I am trying to use MSBuild to build a solution with a specified target platform (I need both binaries, x86 and x64). This is how I tried it:

C:\WINDOWS\Microsoft.NET\Framework\v3.5>MsBuild SolutionPath\Solution.sln /t:Rebuild /p:Configuration=Release /p:Platform="x86"

However the build always fails if the platform is different from "Any CPU". What am I doing wrong?

This is the while output MSBuild prints:

> C:\WINDOWS\Microsoft.NET\Framework\v3.5>MsBuild > SolutionPath\Solution.sln /t:Rebuild > /p:Configuration=Release > /p:Platform="x86" Microsoft (R) Build > Engine Version 3.5.30729.1 [Microsoft > .NET Framework, Version > 2.0.50727.3082] Copyright (C) Microsoft Corporation 2007. All rights > reserved. > > Build started 1.7.2010 8:28:10. > Project "SolutionPath\Solution.sln" on > node 0 (Rebuild targe t(s)). > SolutionPath\Solution.sln : error > MSB4126: The specified sol ution > configuration "Release|x86" is > invalid. Please specify a valid > solution c onfiguration using the > Configuration and Platform properties > (e.g. MSBuild.exe Solution.sln > /p:Configuration=Debug > /p:Platform="Any CPU") or leave those > prope rties blank to use the default > solution configuration. Done Building > Project "SolutionPath\Solution.sln" > (Rebuild t arget(s)) -- FAILED. > > > Build FAILED. > > "SolutionPath\Solution.sln" (Rebuild > target) (1) -> > (ValidateSolutionConfiguration target) > -> SolutionPath\Solution.sln : error MSB4126: The specified s olution > configuration "Release|x86" is > invalid. Please specify a valid > solution configuration using the > Configuration and Platform properties > (e.g. MSBuild.ex e Solution.sln > /p:Configuration=Debug > /p:Platform="Any CPU") or leave those > pro perties blank to use the default > solution configuration. > > 0 Warning(s) > 1 Error(s) > > Time Elapsed 00:00:00.03

If I try to build it for x86/x64 with devenv it works perfectly, however I am trying to set up a build server without installing all the necessary versions of Visual Studio. By the way, if there is a better free tool (that supports .NET framework 4) out there, I'd love to hear about it.

Msbuild Solutions


Solution 1 - Msbuild

In MSBuild or Teamcity use command line

MSBuild yourproject.sln /property:Configuration=Release /property:Platform=x64

or use shorter form:

MSBuild yourproject.sln /p:Configuration=Release /p:Platform=x64

However you need to set up platform in your project anyway, see the answer by Julien Hoarau.

Solution 2 - Msbuild

If you want to build your solution for x86 and x64, your solution must be configured for both platforms. Actually you just have an Any CPU configuration.

How to check the available configuration for a project

To check the available configuration for a given project, open the project file (*.csproj for example) and look for a PropertyGroup with the right Condition.

If you want to build in Release mode for x86, you must have something like this in your project file:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
  ...
</PropertyGroup>

How to create and edit the configuration in Visual Studio

Configuration Manager panel
(source: microsoft.com)

New solution platform button
(source: msdn.com)

New solution platform panel
(source: msdn.com)

How to create and edit the configuration (on MSDN)

Solution 3 - Msbuild

If you're trying to do this from the command line, you may be encountering an issue where a machine-wide environment variable 'Platform' is being set for you and working against you. I can reproduce this if I use the VS2012 Command window instead of a regular windows Command window.

At the command prompt type:

> set platform

In a VS2012 Command window, I have a value of 'X64' preset. That seems to interfere with whatever is in my solution file.

In a regular Command window, the 'set' command results in a "variable not defined" message...which is good.

If the result of your 'set' command above returns no environment variable value, you should be good to go.

Solution 4 - Msbuild

Hopefully this helps someone out there.

For platform I was specifying "Any CPU", changed it to "AnyCPU" and that fixed the problem.

msbuild C:\Users\Project\Project.publishproj /p:Platform="AnyCPU"  /p:DeployOnBuild=true /p:PublishProfile=local /p:Configuration=Debug

If you look at your .csproj file you'll see the correct platform name to use.

Solution 5 - Msbuild

For VS2017 and 2019... with the modern core library SDK project files, the platform can be changed during the build process. Here's an example to change to the anycpu platform, just before the built-in CoreCompile task runs:

<Project Sdk="Microsoft.NET.Sdk" >

  <Target Name="SwitchToAnyCpu" BeforeTargets="CoreCompile" >
    <Message Text="Current Platform=$(Platform)" />
    <Message Text="Current PlatformTarget=$(PlatformName)" />
    <PropertyGroup>
      <Platform>anycpu</Platform>
      <PlatformTarget>anycpu</PlatformTarget>
    </PropertyGroup>
    <Message Text="New Platform=$(Platform)" />
    <Message Text="New PlatformTarget=$(PlatformTarget)" />
  </Target>

</Project>

In my case, I'm building an FPGA with BeforeTargets and AfterTargets tasks, but compiling a C# app in the main CoreCompile. (partly as I may want some sort of command-line app, and partly because I could not figure out how to omit or override CoreCompile)

To build for multiple, concurrent binaries such as x86 and x64: either a separate, manual build task would be needed or two separate project files with the respective <PlatformTarget>x86</PlatformTarget> and <PlatformTarget>x64</PlatformTarget> settings in the example, above.

Solution 6 - Msbuild

When you define different build configurations in your visual studio solution for your projects using a tool like ConfigurationTransform, you may want your Teamcity build, to build you a specified build configuration. You may have build configurations e.g., Debug, Release, Dev, UAT, Prod etc defined. This means, you will have MSBuild Configuration transformation setup for the different configurations. These different configurations are usually used when you have different configurations, e.g. different database connection strings, for the different environment. This is very common because you would have a different database for your production environment from your playground development environment.

They say a picture is worth a thousand words, please see the image below how you would specify multiple build configurations in Teamcity.

In the commandline input text box, specify as below

/p:OutputPath=Publish;Configuration=Dev

Here, I have specified two commandline build configurations/arguments OutputPath and build Configuration with values Publish and Dev respectively, but it could have been, UAT or Prod configuration. If you want more, simply separate them by semi-colon,;

Teamcity MSBuild Commandline build configuration screenshot

Solution 7 - Msbuild

There is an odd case I got in VS2017, about the space between ‘Any’ and 'CPU'. this is not about using command prompt.

If you have a build project file, which could call other solution files. You can try to add the space between Any and CPU, like this (the Platform property value):

<MSBuild Projects="@(SolutionToBuild2)" Properties ="Configuration=$(ProjectConfiguration);Platform=Any CPU;Rerun=$(MsBuildReRun);" />

Before I fix this build issue, it is like this (ProjectPlatform is a global variable, was set to 'AnyCPU'):

<MSBuild Projects="@(SolutionToBuild1)" Properties ="Configuration=$(ProjectConfiguration);Platform=$(ProjectPlatform);Rerun=$(MsBuildReRun);" />

Also, we have a lot projects being called using $ (ProjectPlatform), which is 'AnyCPU' and work fine. If we open proj file, we can see lines liket this and it make sense.

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">

So my conclusion is, 'AnyCPU' works for calling project files, but not for calling solution files, for calling solution files, using 'Any CPU' (add the space.)

For now, I am not sure if it is a bug of VS project file or MSBuild. I am using VS2017 with VS2017 build tools installed.

Solution 8 - Msbuild

In Visual Studio 2019, version 16.8.4, you can just add

<Prefer32Bit>false</Prefer32Bit>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid BožjakView Question on Stackoverflow
Solution 1 - MsbuildTomas KubesView Answer on Stackoverflow
Solution 2 - MsbuildJulien HoarauView Answer on Stackoverflow
Solution 3 - MsbuildJJMplsView Answer on Stackoverflow
Solution 4 - MsbuildaduexjrView Answer on Stackoverflow
Solution 5 - MsbuildgojimmypiView Answer on Stackoverflow
Solution 6 - MsbuildJulius DepullaView Answer on Stackoverflow
Solution 7 - MsbuildVictor XieView Answer on Stackoverflow
Solution 8 - MsbuildArtur MustafinView Answer on Stackoverflow