ChatGPT解决这个技术问题 Extra ChatGPT

Difference between Rebuild and Clean + Build in Visual Studio

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?


J
Jon Coombs

Rebuild = Clean + Build (usually)

Notable details:

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. All three actions correspond to MSBuild targets. So a project can override the Rebuild action to do something completely different.


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.
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 :)
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.
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.
Maybe since this post things have changed, but there is an explanation why Rebuild is not (no longer?) the same as clean + build stackoverflow.com/questions/3095901/…
L
LeopardSkinPillBoxHat

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.


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.
+1 Note also that Rebuild behaviour doesn't seem to be consistent across languages with respect to dependencies: stackoverflow.com/questions/12163080/…
99% of time is vastly overestimated.
P
Palec

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

Per project: Rebuild project = Clean project + Build project. 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.


This one is the most exact answer, since it explains why sometimes I couldn't rebuild, but was able to clean + build.
E
Eduardo Mello

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.


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.
C
Community

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.


N
NoJoshua

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