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
11
votes
2 answers

CLion doesn't resolve headers from external library

Some time ago I started a big header library in C++1x using XCode. The current layout of the library is () something like (partial output from ls -R sponf) sponf/sponf: ancestors sponf.h sponf_utilities.h categories …
gianluca
  • 337
  • 1
  • 3
  • 15
8
votes
1 answer

Install header-only library with Python

I have a header-only C++ library that I use in my Python extensions. I would like to be able to install them to Python's include path, such that I can compile extensions very easily with python3 setup.py build. I'm partly able, but there are two…
Tom de Geus
  • 5,625
  • 2
  • 33
  • 77
8
votes
1 answer

Optional header only library

I'd like to write a C++ library which is not-header-only by default but could be used as a header only library defining a NOLIB macro. I've seen two approaches: inline definitions foo.h #if !defined(FOO_H) #define FOO_H #if defined(NOLIB) # …
manlio
  • 18,345
  • 14
  • 76
  • 126
7
votes
3 answers

How come the fmt library is not header-only?

I know it is possible to use the fmt formatting library in header-only mode: How to use fmt library in the header-only mode? but - why isn't it just header-only, period? That is, what's the benefit of using it in non-header-only mode?
einpoklum
  • 118,144
  • 57
  • 340
  • 684
5
votes
3 answers

using `[[gnu::noinline]]` in header-only library

Functions in a header-only library should be declared as inline to prevent multiple definitions in the different translation units. That is, for example, I wrote a header-only library mylib.hpp: void do_something(int) {} And I used this header in…
陈浩南
  • 633
  • 4
  • 12
5
votes
1 answer

Proper setter and getter for static member variable in header-only library

I have a few small header-only libraries (the header-only part is important). In the initial versions, I had some static members in the classes defined therein. It didn't occur to me until later (when I used them in a bigger project) that the static…
cantordust
  • 1,542
  • 13
  • 17
5
votes
1 answer

Is it possible to use Boost serialization as a header only library?

Below is a minimal example to use the great Boost.Serialization library. To compile the library I need to link with the boost_serialization precompiled library. $ c++ -std=c++11 example.cpp -o example.x -lboost_serialization …
alfC
  • 14,261
  • 4
  • 67
  • 118
5
votes
0 answers

Structuring Single-File/Header-Only Libraries in C++

Is it considered good practice to structure single-file/header-only libraries in C++ such that they are conditionally either the header or the implementation? For example, #ifndef LIBRARY_HEADER_HPP_ #define LIBRARY_HEADER_HPP_ // Header struct…
AUD_FOR_IUV
  • 473
  • 1
  • 3
  • 16
5
votes
2 answers

Header-only and static-inline-only library in C

I write small header-only and static-inline-only libraries in C. Would this be a bad idea when applied to big libraries? Or is it likely that the running time will be faster with the header-only version? Well, without considering the obvious…
user3810155
5
votes
4 answers

Is this too much code for a header only library?

It seems like I had to inline quite a bit of code here. I'm wondering if it's bad design practice to leave this entirely in a header file like this: #include #include #include #include…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
4
votes
1 answer

C++ header-only library with waf

Good day, before fully migrating to waf (1.7.5), I have tried to create a simple project of this structure: wafproject ├── application │ ├── main.cpp │ └── wscript ├── library1 │ ├── foo1.hpp │ ├── foo2.hpp │ └── wscript └── wscript This…
user21919
  • 41
  • 3
3
votes
3 answers

A "source-less" C++ idiom

I am developing a fairly large C++ support library, and have found myself moving towards a header-only approach. In C++ this almost works because you can implement where you define in classes. For templated methods, the implementation has to be in…
user4938472
  • 145
  • 8
3
votes
4 answers

C++ header-only include pattern

I would like to write code in .hpp without separation to .h and .cpp I did it. I use .cpp only for static class-fields definitions I would like not to write #include manually ... I use forward delarations where it possible. Every my .hpp file…
k06a
  • 17,755
  • 10
  • 70
  • 110
3
votes
2 answers

Advice needed: does it make sense to include fmt lib in header-only library?

I'm currently ending the development of a C++ header-only template library for grid-based quantum computations and I'm considering replacing an old logging module that I've wrote nearly at the beginning. I know that it sounds a little bit weird to…
Ranza
  • 294
  • 3
  • 10
3
votes
1 answer

Adding Eigen library to c++ project using cmake

Unfortunately, i have some problems adding the headers only Eigen 3.3.7 Library to my Makefile with Cmake on my Ubuntu 18.04.4 LTS system. I can compile my code using the library by just copying the library folder in the include directory and…
Manumerous
  • 455
  • 6
  • 21