简体   繁体   中英

Separate compilation in C++

Suppose you are creating a class with multiple.cpp files (which each contain the implementation of a member function) and have the class' declaration in ah file. Also, each.cpp file includes the.h file via the include directive.

I was told that if you change the implementation of any of the member functions (.cpp files) that you will have to recompile every.cpp file in order to run the program. That is, if I had 5 member functions (each implemented in a.cpp file) and I changed the implementation of 1 of the.cpp files I would have to compile the 1.cpp file I changed AND the 4 other.cpp files I didn't change in order to correctly run my program.

My question, if the previous statement is true, is why is the statement is true? Any insight on this concept would be helpful.

It's false. If there is no change to a particular implementation file, no change to header files it includes, and no changes to the environment or compiler options, there is absolutely no need to recompile it. Everything that affects the compilation of that file hasn't changed.

In fact, you can compile each of the files without even having any of the others at all. Then you can link all the compiled files together without all the implementation files ever having been in the same place.

The statement is false. In fact, the whole reason make (and similar) exist is because it's false -- they minimize the time/effort to rebuild an application by keeping track of which source files have changed, and re-creating only the object files that depend on source files that have changed. Object files that are up to date are simply left alone.

Once all the object files are up to date, they're linked together to produce the final executable (and, again, this is normally only done when/if at least one object file is newer than the current executable).

Of course, it's also possible to use make for jobs unrelated to compiling and linking, but that is almost certainly the use to which they're most commonly put, and (at least most of) the reason they were invented (the only obvious change being that they were originally used primarily for C source code rather than C++, but in this respect the two are nearly indistinguishable).

The only obvious difference between the two is that when/if you use templates, you typically end up putting a lot of code into headers. In this case, changing a header forces re-compiling all the code that includes that header, which is often a lot. With C code and/or non-template C++ code, you put most of the code into a source file, so you only need to re-compile other files if you change the interface to the code (which would typically change the header), but only that file needs to be re-compiled if you restrict your changes to the implementation without changing the interface.

As others have said, the statement is false. However, there do exist interesting complications regarding modified source files, source dependencies, and recompilation. The most helpful discussions of this are, unsurprisingly, given in a series of Guru of the Week posts from Herb Sutter, in which he discusses dependency isolation and the Pimpl idiom:

Compilation Firewalls

The Fast Pimpl Idiom

The Joy of Pimpls

This stuff appears esoteric to almost everyone the first time they see it but the Pimpl idiom turns out to be astonishingly useful in practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM