Questions tagged [header-only]

In C or C++, 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.

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.

The disadvantages include:

  • brittleness. Most changes to the library will require recompilation of all compilation units using that library
  • longer compilation times. The compilation unit must see the implementation of all components in the included files, rather than just their interfaces

The header-only form is popular because it avoids the problem of packaging.

87 questions
181
votes
5 answers

Benefits of header-only libraries

What are the benefits of a header only library and why would you write it that way oppose to putting the implementation into separate file?
NebulaFox
  • 7,813
  • 9
  • 47
  • 65
92
votes
5 answers

In CLion, header only library: file "does not belong to any project target, code insight features might not work properly"

I have a header-only library project set up with the cmake command: add_library(my_library INTERFACE) and I also added target_sources(my_library INTERFACE ${MY_LIRBARY_HEADER_FILES}) but when I open a source file, I get the warning: This file…
xaxxon
  • 19,189
  • 5
  • 50
  • 80
68
votes
1 answer

CMake: target_include_directories() prints an error when I try to add the source directory itself, or one of its subdirectories

I am writing a C++ library (header-only) and am using CMake to generate my (Visual Studio) project and solution files. I'm also writing a test suite, which is part of the same CMake project. My problem occurs when I call target_include_directories()…
JPNotADragon
  • 2,050
  • 2
  • 25
  • 31
64
votes
3 answers

How to have static data members in a header-only library?

What is the best way to have a static member in a non-templated library class, without placing the burden of defining the member on the class user? Say I want to provide this class: class i_want_a_static_member { static expensive_resource…
pesche
  • 3,054
  • 4
  • 34
  • 35
55
votes
4 answers

How do I create a header-only library?

I'd like to package a library I'm working on as a header-only library to make it easier for clients to use. (It's small and there's really no reason to put it into a separate translation unit) However, I cannot simply put my code in headers because…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
45
votes
3 answers

Quantifiable metrics (benchmarks) on the usage of header-only c++ libraries

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…
Homer6
  • 15,034
  • 11
  • 61
  • 81
32
votes
7 answers

Library design: allow user to decide between "header-only" and dynamically linked?

I have created several C++ libraries that currently are header-only. Both the interface and the implementation of my classes are written in the same .hpp file. I've recently started thinking that this kind of design is not very good: If the user…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
25
votes
4 answers

When should I consider making a library header-only?

Obviously template libraries need to be header only, but for non-templates, when should you make things header-only?
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
15
votes
4 answers

Is it ever impossible to write a header-only library?

Is there ever such a pattern of dependancies that it is impossible to keep everything in header files only? What if we enforced a rule of one class per header only? For the purposes of this question, let's ignore static things :)
sooniln
  • 14,607
  • 4
  • 29
  • 35
14
votes
2 answers

Why is Boost.ProgramOptions not header-only?

Some boost libraries are header-only, some are not, and for various reasons etc. Is there a specific reason/design decision why Boost.ProgramOptions is not header-only? I'm wondering because it claims to be a "small" library in its the documentation…
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
14
votes
3 answers

CMake: header-only library with generated files

I have a library that needs to carry some constant data injected from the content of non-source files (in this case, OpenGL shader code). To achieve this, I'm using add_custom_command() to generate include files that I can then #include into my code…
JPNotADragon
  • 2,050
  • 2
  • 25
  • 31
14
votes
2 answers

Static data in header-only libraries

I am developing a library that will consist of header files only. So far, it contains only classes, which has been fine. However, I have come to a point where I need to have some library-wide accessible unchanging data in the library (that is, not…
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
13
votes
9 answers

C++ header-only template library

Looking at this project (http://www.savarese.com/software/libssrckdtree/) I found the definition "C++ header-only template library". At the moment I have basic C++ knowledge but would like to know what this exactly means and why this people use it…
Open the way
  • 26,225
  • 51
  • 142
  • 196
12
votes
3 answers

What's the proper way of using header-only library?

I've ran into a confusion about how to properly use header-only library. Googling didn't help as I didn't find anything about using header-only libraries. So my question is: Should I just copy the header files and paste them into my project folder…
Stefan Dimeski
  • 438
  • 4
  • 16
11
votes
1 answer

Double inclusion and headers only library stbi_image

I have a main.cpp including a.h (that has its own a.cpp) a.h includes the header only library "stbi_image.h" as such: #ifndef STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION #include…
cppbeginner
  • 111
  • 1
  • 1
  • 3
1
2 3 4 5 6