6 ms·
not a C++ person here but is header-only library an advantage?
by yread 8y ago
not a C++ person here but is header-only library an advantage?
- steveklabnik 8y agoIt's generally considered one, yes.
- ketzu 8y agoIn C++ header-only libraries are nice as they do not need a separate compile and linking step.
- orbifold 8y agoYes because there is no standardised build system for C++, which makes integrating non-header only libraries a pain (or at least somewhat more painful), especially if your aim is cross platform code. In that case you can not rely on a reasonable package manager being present and will have to essentially include all your dependencies in the build, this is trivial for header only libraries.
- StreamBright 8y agoIs there a reason not to have a standard build system?
- asveikau 8y agoOverstated problem. It's not that hard under most circumstances to build a static lib. I think there are two issues. In C++, a header only library makes sense due to templates, which don't produce object code before specialization. In C, I have seen the same with entirely macro-based "libraries" that are really simulating templates in order to make abstract data structures. The second case though, my theory is that there is a new generation of programmers who don't understand the traditional compile and link phases because they are used to other languages. It doesn't fit their expectation of how it should work so they bend the tool to their expectation instead of figuring out the old way. The very fact that people on here are saying a programming language should have a "standard package manager" demonstrates the cultural divide: this sounds a little nutty to an old time C person.
- anonknowsaguy 8y agoAs newer generation C++ programmer, can confirm I'm guilty of writing things header only when they "probably shouldn't be". But it's not so much I don't understand the old way. Having everything in one file is just so much easier to work with. The price you pay for it is relatively low(for small projects like this library).
- rkangel 8y agoThe advantage is how easy they are to integrate - just put the file in your tree and then #include. No build option or compiler issues. The disadvantage (and the reason that I'm personally not a fan) is that they bloat compile time. If it's a library you compile it once, and then it gets linked repeatedly, but a header only library is compiled every time any CPP file that includes it is compiled.
- makecheck 8y ago“Compiled every time” is not really true if the header correctly contains “#pragma once” or #ifdef guards. It may be that you have used a lot of template-based headers, which may compile nearly every time because they are literally creating new code every time a new combination of template parameters is given.
- rkangel 8y agoIt'll be compiled once for every Cpp file that includes it. Pragma once (or ifdefs) means that it only gets included once for that compilation, and has no effect on any other Cpp files. You are correct that I'm mostly talking about template heavy header files. There is a strong correlation between template based header files and header only libraries. The matter) latter generally means the former.
- jcelerier 8y ago> It'll be compiled once for every Cpp file that includes it. but are you going to mmap stuff in all your .cpps ? Most of the time when I use an external library, it does not get out of a single implementation file, so it being header only does not really make it worse
- paavoova 8y agoYou still have to compile the library each time you recompile whichever source file includes it. If the library had its own source file, you would only have to compile once and link it thereafter. This adds up if you have a lot of header-only libraries in your code base, and the total LoC may very well dwarf your own codebase. This is where precompiled headers come in, but introduce their own downsides.
- dima55 8y agoIf you're the sort of person that C++ is a good language then yes. Otherwise, no. It has exactly the issues you think it has.
- Fronzie 8y agoSetting up the dependency infrastructure is a bit of work in C++. Recently things have been moving forward, with tools like Cmake+Conan or vcpkg becoming more popular. Once the infrastructure is sorted out, header-only has mainly disadvantages, like making it harder to separate interface from implementation. There's a good talk on the topic: https://youtu.be/sBP17HQAQjk https://youtu.be/sBP17HQAQjk