Search code examples
visual-studiovisual-c++msbuildsln-file

When do I need to pass MSBuild a solution file?


From the command line, when would I call MSBuild directly on a project, and when would I call MSBuild on the projec's sln file, passing /t:Build /t:ProjectName?

Example: I have a simple solution containing several projects (A, B, C, ...). Development is done through the VS GUI, by opening and working with the solution.

Now, in a specific automated case, I want to build just project B from the command line:

What should I call?:

(a) MSBuild "my.sln" "/t:Build" "/t:ProjB" "/p:Configuration=Release" "/p:Platform=Any CPU"

(b) MSBuild "ProjB.vcxproj" "/t:Build" "/p:Configuration=Release" "/p:Platform=Any CPU"

  • Will there be any difference in the outcome?
  • Could there be any additional setting in the sln file that gets missed this way? (I certainly don't see any additional info in our VS2015 sln file.)
  • If the solution is large, but ProjB has little interdependencies with other projects in the solution, will one option be faster in general?

Solution

  • I can, so far, identify these issues:

    • Quite obviously, if you have additional sln based dependencies, these will simply be ignored.
      • In this day and age, you really should work with project2project dependencies, but I think there's some semi valid reasons to stick with solution deps in some cases.
    • Macro Values. Starting with $(SolutionFileName)and quite everything else that starts with solution. If your project, or its dependencies, use any of these: subtly different result may ensue.
    • Platform. Oh my. You see, they way solution platforms work, they're basically just a named container to group together project platform selections.

      What this means is, esp. for C++, that when invoking the sln file it might very well be that you need to specify a different platform. For example, with the solution, you'd build 'Mixed Platforms' or maybe 'Any CPU', but for the project you actually need to specify x64 or Win32.

      So the Platform that needs to be passed might have to differ.