Difference between Rebuild and Clean + Build in Visual Studio
Asked Answered
L

7

325

What is the difference between just a Rebuild and doing a Clean + Build in Visual Studio 2008? Is Clean + Build different then doing Clean + Rebuild?

Lem answered 7/8, 2009 at 23:32 Comment(0)
C
323

Rebuild = Clean + Build (usually)

Notable details:

  1. For a multi-project solution, "rebuild solution" does a "clean" followed by a "build" for each project (possibly in parallel). Whereas a "clean solution" followed by a "build solution" first cleans all projects (possibly in parallel) and then builds all projects (possibly in parallel). This difference in sequencing of events can become significant when inter-project dependencies come into play.

  2. All three actions correspond to MSBuild targets. So a project can override the Rebuild action to do something completely different.

Cohbath answered 7/8, 2009 at 23:41 Comment(8)
So you are saying that Rebuild is exactly the same as a Clean followed by a Build? That is kind of what I thought, but I wasn't sure.Lem
Except Rebuild cleans and the rebuilds each project one by one. Clean+Build cleans all of them and then builds all of them. Makes difference mostly if you click it by accident :)Howund
Except for the lack of guarantee that they are the same. See JaredPar's answer below which combined with Earl's is the whole picture. Because Rebuild does each project in turn, you can have a "corner case" when your dependency information is messed up and you get an out of order build project of B using the old project A then rebuild A, then rebuild C. etc. A full solution Clean followed by a full solution build will catch this situation while a rebuild won't. So the more paranoid and tired you are, the more you should favor towards Clean then Build.Saxophone
This is not true. I have had a project where Clean + Build succeeded, and Rebuild returned compile errors (circular file references). So they are not 100% the same.Iyar
+1 Yaakov Ellis. I had also thought Rebuild = Clean + Build, but recently discovered that it works differently for C++ (CLR) and C# projects with respect to dependencies: #12163580Pet
Maybe since this post things have changed, but there is an explanation why Rebuild is not (no longer?) the same as clean + build #3096401Sortition
Rebuild != Clean + Build. THEY ARE NOT THE SAME. I have an afterClean action that deletes a plugin directory. If I clean and build, all is fine. If I rebuild, some plugin projects build and copy their dlls to the plugin directory. Then the project that hosts the plugins cleans. See the problem. :-) Rebuild is causing the newly built plugins to be cleaned from the plugin directory immediately after they are built.Supercilious
"This difference in sequencing of events can become significant when inter-project dependencies come into play." This IS an important distinction, and I'd wager it's the most common reason that Rebuild may lead to errors where Clean & Build may not. @Rhyous' example demonstrates one such scenario.Generatrix
B
169

Earl is correct that 99% of the time Rebuild = Clean + Build.

But they are not guaranteed to be the same. The 3 actions (rebuild, build, clean) represent different MSBuild targets. Each of which can be overriden by any project file to do custom actions. So it is entirely possible for someone to override rebuild to do several actions before initiating a clean + build (or to remove them entirely).

Very much a corner case but pointing it out due to comment discussions.

Blaze answered 8/8, 2009 at 16:3 Comment(3)
If you add build actions that move .dll files to a new location you can get 'bad' build results by just doing a rebuild. If you mix a vb.net and c# project things get even worse, broken form designers and no working goto.Lole
+1 Note also that Rebuild behaviour doesn't seem to be consistent across languages with respect to dependencies: #12163580Pet
99% of time is vastly overestimated.Supercilious
G
65

Let's define default Rebuild implementation in terms of default Clean and Build implementations:

  1. Per project: Rebuild project = Clean project + Build project.

  2. Per solution: Rebuild sln = foreach project in sln (Clean project + Build project).

Note that due to differences in the order of execution, Rebuild sln is not the same as (Clean sln + Build sln) = (foreach project in sln Clean project) + (foreach project in sln Build project). Also, this "foreach" may execute concurrently, so different tasks are allowed to run concurrently in the two scenarios.

Say you have a sln that contains proj1, proj2, and proj3.

  • Rebuild sln = (Clean proj1 + Build proj1) & (Clean proj2 + Build proj2) & (Clean proj3 + Build proj3)

  • Clean Sln + Build Sln = (Clean proj1 & Clean proj2 & Clean proj3) + (Build proj1 & Build proj2 & Build proj3)

+ means serial, & means concurrent.

So if project dependencies are not configured correctly, there is a chance that when you execute Rebuild sln, some of your projects link to a stale lib. That's because all cleans are not guaranteed to be finished before the first build starts. If you execute Clean sln + Build sln, they will give a link error and let you know that immediately, instead of giving you an app with odd behavior.

Groundsel answered 17/2, 2012 at 3:57 Comment(1)
This one is the most exact answer, since it explains why sometimes I couldn't rebuild, but was able to clean + build.Keeley
T
11

From http://www.cs.tufts.edu/r/graphics/resources/vs_getting_started/vs_getting_started.htm, (just googled it):

Build means compile and link only the source files that have changed since the last build, while Rebuild means compile and link all source files regardless of whether they changed or not. Build is the normal thing to do and is faster. Sometimes the versions of project target components can get out of sync and rebuild is necessary to make the build successful. In practice, you never need to Clean.

Build or Rebuild Solution builds or rebuilds all projects in the your solution, while Build or Rebuild builds or rebuilds the StartUp project, "hello" in the screen shot above. To set the StartUp project, right click on the desired project name in the Solution Explorer tab and select Set as StartUp project. The project name now appears in bold. Since the homework solutions typically have only one project, Build or Rebuild Solution is effectively the same as Build or Rebuild .

Compile just compiles the source file currently being edited. Useful to quickly check for errors when the rest of your source files are in an incomplete state that would prevent a successful build of the entire project. Ctrl-F7 is the shortcut key for Compile.

Thurston answered 21/8, 2009 at 13:10 Comment(1)
Like Toan Nguyen, I've experienced that sometimes Clean+Build Solution succeeds where a Rebuild Solution fails (probably due to inter-project dependencies), so this answer is misleading, at least in 2018.Jimjimdandy
H
4

From this blog post which the author linked as a comment on this question:

Actually No!!! they are not equal.

The difference is in the sequence projects get clean and build. Let say we have two projects in a solution. Clean and then build will perform clean to both projects and then build will occur individually while on rebuild project A will get and clean and then build after that project B will be clean and then build and so on.

Hecker answered 15/12, 2014 at 8:37 Comment(0)
E
1

Documentation for Visual Studio 2022 explains:

  • Choose Build or Build Solution, or press Ctrl+Shift+B, to compile only those project files and components that have changed since the most recent build. The Build command becomes Build Solution when a solution includes more than one project.
  • Choose Rebuild Solution to "clean" the solution and then build all project files and components.
  • Choose Clean Solution to delete any intermediate and output files. With only the project and component files left, new instances of the intermediate and output files can then be built.

build-configuration-manager-menu

This, and more information, here: https://learn.microsoft.com/en-us/visualstudio/ide/building-and-cleaning-projects-and-solutions-in-visual-studio?view=vs-2022

Erinaceous answered 12/9, 2022 at 11:9 Comment(0)
S
0

Another difference: Clean clears the test results in Test Explorer, which Rebuild doesn't.

Stacee answered 5/3, 2021 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.