Header-only
In the context of the C or C++ programming languages, a library is called header-only if the full definitions of all macros, functions and classes comprising the library are visible to the compiler in a header file form, with no separate implementation files or precompiled binaries.[1][2] Header-only libraries do not need to be separately compiled, packaged and installed in order to be used. All that is required is to point the compiler at the location of the headers, and then #include the header files into the application source. Another advantage is that the compiler's optimizer can do a much better job when all the library's source code is available. Because the compiler has direct access to the implementation, it can inline more code, which can make the compiled executable faster.[2]
Compiling the library's code directly allows for more efficient inlining, potentially resulting in faster executable code due to the elimination of function call overheads.[2]
The disadvantages include:
- Brittleness: most changes to the library will require recompilation of all compilation units using that library, which can further increase development time.
- Longer compilation times: the compilation unit must see the implementation of all components in the included files, rather than just their interfaces. This can lead to increased compilation times, especially for large libraries or projects that include the library in multiple files.[2]
- Machine-code bloat: the necessary use of inline statements in non-class functions can lead to code bloat by over-inlining,
- Harder to understand: the header contains both the interface and the implementation whereas traditional library separate both.
Nonetheless, the header-only form is popular because it simplifies code distribution and re-use by avoiding many of the problems associated with packaging and linking external code.
For C++ templates, including the definitions in header is the only way to compile, since the compiler needs to know the full definition of the templates in order to instantiate.
See also
References
- ^ Wilson, Matthew (2004). Imperfect C++. Addison-Wesley. p. 177. ISBN 0-321-22877-4.
- ^ a b c d Danilov, Dmitrij A. (2024). Refactoring with C++. Packt Publishing. ISBN 978-1-83763-941-0.
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.