问题
Suppose a have a fairly complex class I'm working on. Half the methods are done and tested, but I'm still devolping the other half. If I put the finished code in one cpp and the rest in another, will Visual Studio (or any other IDE for that matter) compile faster when I only change code that's in the "work-in-progress" cpp?
Thanks!
回答1:
It really depends. For a very large project, link time can often be considerably more expensive than the time to compile a single file. In our codebase at work (a game based on the Unreal Engine) we actually found that making "bulk.cpp" files that include many other files (effectively fewer translation units) decreases the turn around time significantly.
Even though individual compile time for a small change was increased, overall compile time (full rebuild) and link time (which happens even for a small change) both decreased dramatically.
回答2:
Yes, I believe Visual Studio compiles incrementally, so as long as you hit Build and not Rebuild All you should get faster compile times by splitting out.
However, you should really be splitting out because of code-factoring reasons i.e. each class should have a single purpose etc. etc... I'm sure you know.
回答3:
As long as the header file doesn't change (assuming both .cpp's include the same header) then only the changed .cpp files will be compiled.
This is true for most IDEs at the very least. I haven't had experience in directly invoking compilers like gcc so I can't comment on that.
回答4:
The answer is probably yes because you'll probably be performing incremental builds (compiling only the .cpp that changes) and pre-compiled headers. If you are not using either of these features, then you'll get slower builds. I'm pretty sure that a default Visual Studio C++ projects uses both incremental builds and pre-compiled headers.
回答5:
Yes it will be faster.
But more importantly: Don't worry about this, if your class is so large that it's taking a lot of time on a modern processor it's god's way of saying your class needs to get refactored into smaller pieces.
来源:https://stackoverflow.com/questions/1501446/will-splitting-code-into-several-cpps-decrease-compilation-time