I've tried to find an answer to this using SO. There are a number of questions that list the various pros and cons of building a header-only library in c++, but I haven't been able to find one that does so in quantifiable terms.
So, in quantifiable terms, what's different between using traditionally separated c++ header and implementation files versus header only?
For simplicity, I'm assuming that templates are not used (because they require header only).
To elaborate, I've listed what I have seen from the articles to be the pros and cons. Obviously, some are not easily quantifiable (such as ease of use), and are therefore useless for quantifiable comparison. I'll mark those that I expect quantifiable metrics with a (quantifiable).
Pros for header-only
- It's easier to include, since you don't need to specify linker options in your build system.
- You always compile all the library code with the same compiler (options) as the rest of your code, since the library's functions get inlined in your code.
- It may be a lot faster. (quantifiable)
- May give compiler/linker better opportunities for optimization (explanation/quantifiable, if possible)
- Is required if you use templates anyways.
Cons for header-only
- It bloats the code. (quantifiable) (how does that affect both execution time and the memory footprint)
- Longer compile times. (quantifiable)
- Loss of separation of interface and implementation.
- Sometimes leads to hard-to-resolve circular dependencies.
- Prevents binary compatibility of shared libraries/DLLs.
- It may aggravate co-workers who prefer the traditional ways of using C++.
Any examples that you can use from larger, open source projects (comparing similarly-sized codebases) would be very much appreciated. Or, if you know of a project that can switch between header-only and separated versions (using a third file that includes both), that would be ideal. Anecdotal numbers are useful too because they give me a ballpark with which I can gain some insight.
sources for pros and cons:
- https://mcmap.net/q/138279/-c-header-only-template-library
- https://mcmap.net/q/138280/-what-are-the-advantages-and-disadvantages-of-implementing-classes-in-header-files
Thanks in advance...
UPDATE:
For anyone that may be reading this later and is interested in getting a bit of background information on linking and compiling, I found these resources useful:
- Chapter 7 of http://www.amazon.com/Computer-Systems-Programmers-Perspective-Edition/dp/0136108040
- http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
- http://www.cyberciti.biz/tips/linux-shared-library-management.html
UPDATE: (in response to the comments below)
Just because answers may vary, doesn't mean that measurement is useless. You have to start measuring as some point. And the more measurements you have, the clearer the picture is. What I'm asking for in this question is not the whole story, but a glimpse of the picture. Sure, anyone can use numbers to skew an argument if they wanted to unethically promote their bias. However, if someone is curious about the differences between two options and publishes those results, I think that information is useful.
Has no one been curious about this topic, enough to measure it?
I love the shootout project. We could start by removing most of those variables. Only use one version of gcc on one version of linux. Only use the same hardware for all benchmarks. Do not compile with multiple threads.
Then, we can measure:
- executable size
- runtime
- memory footprint
- compile time (for both entire project and by changing one file)
- link time
<Foundation/Foundation.h>
(Approx 100k lines of code), using a PCH over a normal header can increase the build times by about 2x. – Erelong