67

I'm running msbuild from the command line with the following:

msbuild mysolution.sln -p:outputdir=c:\mydir

When I run this, the outputdir is being ignored and the default specified in the csproj file is being used.

The MSDN doc for this tool says that I should be able to override the build directory using this parameter. What am I doing wrong?

Mike
  • 1,233
  • 1
  • 10
  • 14
  • 1
    I made an error here with using -p rather than /p. The MSDN doco for the MSBuild Command Line Reference (http://msdn.microsoft.com/en-us/library/ms164311.aspx) incorrectly refers to outputdir. – Mike Feb 13 '11 at 22:46
  • You should make your comment and answer instead. – MX D Oct 26 '16 at 16:29

3 Answers3

139

You should use OutputPath and more important you should use the right syntax :

msbuild mysolution.sln /p:OutputPath=c:\mydir
Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117
  • Thanks for your help on this one. I think you mean the /p rather than -p when you referred to the syntax problem. I was actually using the /p in cmd.exe but got it wrong here - too much PowerShell. As for the Outputpath, dead on. Thanks. – Mike Feb 13 '11 at 22:45
  • It also worked for me.. I was usint /P:outputdir instead of /p:outputpath – Oscar Foley Nov 11 '11 at 15:39
  • 3
    Ensure to leave off the ending `\ `. `/p:OutputPath=c:\mydir` and NOT `/p:OutputPath=c:\mydir\ ` – Jake Berger Mar 22 '12 at 16:00
  • 2
    I tried this, but MSBuild seems to ignore `OutputPath` when the linker output directory is specified in the project options. – Jim Fell Oct 28 '15 at 20:00
  • 6
    If this didn't work, 1: Consider using `OutDir` instead of `OutputPath`. 2: Make sure the `OutDir` is the first thing listed for your `/p:` args. – kayleeFrye_onDeck Jul 19 '17 at 17:41
  • 3
    `OutputPath` had no effect in my case but `OutDir` did it – TorbenJ Dec 11 '19 at 11:53
9

Note that OutputPath is preferred over OutDir. The documentation used to be wrong about this, but I see that they've finally fixed it.

Beyond that, it's difficult to say exactly what the problem is, since you didn't show the exact path that you're passing as a parameter. There are two possible problems that I can imagine:

  1. The OutputPath option specifies the path to the output directory relative to the project directory. That means you can't set it to a global path like C:\mydir. I assume it is unable to find the path you specified, and so it defaults to the one specified in your project file.

  2. If the path that you're actually specifying as a parameter contains spaces, the command is likely to fail. I believe you need to wrap the path in quotes and append an extra backslash to the end of the path string.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Thanks for all your help Cody. The doco I've been reading (The MSBuild Command Line Reference for VS 2010: http://msdn.microsoft.com/en-us/library/ms164311.aspx) uses OutputDir. I'd say we're reading different pages. I've now also built successfully, using an absolute directory reference like c:\mydir. – Mike Feb 13 '11 at 22:43
  • @Mike: These things happen when you document the exact same features in so many different places. I'm not really sure if you're saying that `OutputPath` didn't work for you? `OutDir` *will* still work. – Cody Gray - on strike Feb 14 '11 at 11:50
  • 3
    GUYS. There is no `OutputDir` parameter. There is only `OutputPath` and `OutDir`. Stop adding to the confusion. – Steven Liekens May 04 '15 at 13:40
  • 1
    FYI - It does appear that OutputPath can be an absolute path. – Mark Lopez Jul 17 '19 at 00:43
  • @StevenLiekens I think `OutputDir` is from older versions of MSBuild. I could see that used in my csproj files. They compile just fine with modern VS. – nawfal Nov 12 '19 at 06:01
  • Have you got a reference for "`OutputPath` is preferred over `OutDir`"? According to https://github.com/dotnet/msbuild/issues/87#issuecomment-101467157 it's the other way around. And https://learn.microsoft.com/en-us/cpp/build/reference/common-macros-for-build-commands-and-properties?view=msvc-170 doesn't even include `OutputPath` in the docs. – Mark Ingram Mar 30 '22 at 16:49
2

I believe you should be using OutputPath.

Steve
  • 1,760
  • 10
  • 18