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
3
votes
1 answer

constexpr template meta-info variable in c++11

I'm trying to represent some meta information in a structured way (i.e. using a class). It's header-only and I need to support c++11 so can't use inline variables. I've come up with a couple of potential solutions but each has its drawbacks. Any…
vaind
  • 1,642
  • 10
  • 19
3
votes
1 answer

Visitor-Pattern in C++ in a Header-Only Environment

I've currently trouble implementing the Visitor-Pattern in a Header-Only library in C++. Consider following class which should support being visited by a visitor (no interface for simplicity reasions): class Acceptor { void accept(Visitor* v) …
Bastian
  • 1,553
  • 13
  • 33
3
votes
2 answers

Can I automoc a header only object that inherits QObject?

I'm relatively new to Qt, and I am incorporating it into our projects. I made a very small object that inherits QObject that I just use with a timer. I created a header only file for it but realized the compiler quickly didn't like it. So I created…
Mikey A. Leonetti
  • 2,834
  • 3
  • 22
  • 36
3
votes
1 answer

Inclusion of a header-only library to multiple files producing a linker error

I'm using a header-only single-file "library" to load .obj-models from a file into a graphics program. A simple linker error has popped up: LNK2005 "Info here" already defined in main.obj Now I know what this means and normally I'd proceed with…
Felix
  • 2,548
  • 19
  • 48
3
votes
1 answer

gcov reporting wrong result for header only class

There were already many similar questions asked before: Getting useful GCov results for header-only libraries Why does gcov report 0% coverage on a header file for a well used class? gcov is not generating coverage information for header files to…
sigy
  • 2,408
  • 1
  • 24
  • 55
3
votes
0 answers

Header-Only circular dependency

I am having problems with a circular dependency in a head-only library for C++ that would have not been a circular dependency problem when using source files instead of making everything header-only. The situation is equal to this: There are four…
robbepop
  • 119
  • 10
3
votes
2 answers

CMake ADD_LIBRARY INTERFACE alternative

I am trying to use a header only library (thread-pool) as a sub project. So in root CMakeLists.txt I have ADD_SUBDIRECTORY(thread_pool) inside thread-pool/CMakeLists.txt CMAKE_MINIMUM_REQUIRED(VERSION…
Neel Basu
  • 12,638
  • 12
  • 82
  • 146
3
votes
3 answers

Preventing CMake-generated makefile for optional-header-only library from compiling source files in header-only mode

I have a library that can both be used as an header-only library and as a traditional library. To enable this optional-header-only functionality, the library includes .cpp source files if compiled in header-only mode. Example: // Vector.hpp //…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
3
votes
2 answers

What's wrong with a header-only C++ application?

I'm wondering to code a C++ application with a header-only layout like the following: // code3.h #include class code3 { public: void print() { std::cout << "hello " << std::endl; } }; // code2.h #include "code3.h" class…
Alvaro
  • 37
  • 1
  • 5
2
votes
2 answers

How to make clangd to enable header-only library implementation

There is a way of distributing C/C++ libraries as a one header-only file. Library is in one library.h file: header declarations together with implementation hidden under #ifdef LIBRARY_IMPL. You need to create some .c/.cpp file that does #define…
smokku
  • 1,256
  • 13
  • 22
2
votes
1 answer

Header only library as a module?

I'm authoring a templated header only library. It has no state, no global variables, no .cpp that needs to be compiled. Is it possible to export/consume this as a module? How? What are the benefits? What are the pitfalls? There are some convenience…
non-user38741
  • 671
  • 5
  • 19
2
votes
2 answers

Add interface library as SYSTEM in modern CMake

I'm currently transforming my C++ project from simply using a Makefile to using CMake. As I'm unfamiliar with CMake, I try and avoid learning "old" CMake and stick to "modern CMake" best practices, e.g. described here. However, I could not figure…
mable
  • 61
  • 1
  • 6
2
votes
1 answer

Difference between inlining and including source through macro

I've spent some time developing a header-only library and have stumbled across a crossroads. Everywhere I look on the web, inline functions are always used. But in the example of stb_image.h, the source code is just written in the bottom half of the…
J. Lengel
  • 570
  • 3
  • 16
2
votes
2 answers

C++ header-only with global state in a shared library

I'm working on a C++ library that I would ideally keep header-only. A specific part of this library requires a global state. Let's say it needs a global vector of strings for this example. I can easily achieve this with a static variable inside a…
Macmade
  • 52,708
  • 13
  • 106
  • 123
2
votes
1 answer

CMake: How to handle multiple versions of same libraries?

in my project I am using the header only library rapidjson v1.1.0. └── my_project ├── CMakeLists.txt ├── src │ ├── 3rdParty/tiny_dnn (header only) │ ├── CMakeLists.txt │ ├── src │ └── rapidjson_v0.2 │ └──…