0

In C++, I've heard that you need to separate the implementation from the interface in order to reduce compile times. In this question, the answerer basically says that if you change the header then the source is recompiled, so if you separate the interface from the implementation then there is less chance you will need to change the header. But here are the two scenarios I see:

  1. Have all code in header file

    Whenever you change any code, the entire file is recompiled.

  2. Have code in separate header files and source files

    Since the header file is usually copy and pasted (via include) into the source file, if you make a change to either the header or source files the whole file will still have to be recompiled because it is as if they are one file.

    Whether you change either the header or the source file, the whole file (both the header and the source) are recompiled.

So then what is the advantage of separating interface from implementation? You might say it is so that you can have multiple implementations for a single interface by means of inheritance, but can't you still do that when you have all the code in the header file? You could also say that it is so that the interface does not see any implementation details, but what is the use of that?

The only reason I can see why separating interface from implementation is that if the C++ compiler skips over the part of the source file that is the header file that is copy and pasted into the source code if it is not changed and just compiles the rest of the source file. So is this the case? Do C++ compilers skip compilation of certain parts of files that have not changed? I know that that is probably not what happens, but I cannot think of any other explanation.

Edit

I've already seen this question. What I am asking is why are the compilation times faster with separate implementation and interface files. I understand that there are advantages with separate implementation and interface files such as what Lightness Races in Orbit stated, but I am asking is why are compile times better if they are at all.

Community
  • 1
  • 1
M3579
  • 890
  • 2
  • 11
  • 22
  • 1
    No they don't. Build systems might. Your real question is why are there header files -- see e.g. http://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files-in-c – Ismail Badawi Sep 12 '15 at 15:08
  • I read this: http://www.informit.com/articles/article.aspx?p=26039 and the problem I was having was that I thought that separate interface and implementation files improve compile time for the header and source files. It doesn't. It improves compile time for any other files that include the header files because they don't have to be recompiled when the header changes. – M3579 Sep 12 '15 at 15:32

1 Answers1

2

You missed one crucial fact: you should be minimising changes to header files.

The headers contain your interface, which should remain unchanged for as long as possible to keep your code stable. Most of your changes going forwards ought to be in the implementation files, which may be recompiled in isolation.

The real benefit of separating interface from implementation is so that users of your code/library can be given a distributable that contains your header files, allowing them to interface with your code. They do not need to handle, manage, build etc the far more bulky implementation code. If you are running Linux, look at your /usr/include and /usr/lib folders and you'll see what I mean — all the implementation can be shipped as a compiled binary, which is far less unwieldy (and harder to modify in-place).

Frankly this seems like common sense to me. When you buy a product, you receive with it an instruction manual, not the steps to manufacture it.

It's also next to impossible to mock out the implementation for unit tests if you lump everything together into a single translation unit. The C compilation model was simply not designed for that.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • So basically, if you define a Class.h and Class.cpp, you can give others the Class.h and they can define their own Class.cpp? – M3579 Sep 12 '15 at 15:14
  • @M3579: Yes. And this is the best way to write mocks for unit tests. – Lightness Races in Orbit Sep 12 '15 at 15:15
  • But does separating interface from implementation help compile times as said here: http://programmers.stackexchange.com/questions/48857/ways-to-organize-interface-and-implementation-in-c ? – M3579 Sep 12 '15 at 15:16
  • @M3579: That's too broad a question to answer here. You need to specify some constraints; name some specific scenario. General rule of thumb: if you're asking "is X faster than Y?" your question is not well-enough thought-out. – Lightness Races in Orbit Sep 12 '15 at 15:17