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

When should I use (non-header) source files in modern C++?

The typical way of writing C++ code is to seperate it in header and (non-header) source files. I saw a lot of modern C++ libraries that are header-only (e.g. some Boost libraries). Typically these libraries make a lot of use of templates. Instead of…
Max
  • 63
  • 1
  • 5
2
votes
1 answer

C++ Inline functions and template function in header-only library

In order to avoid code bload due to over-inlining... Is this a valid way to implement a template function that acts like an inline function? Original inline function declaration: inline double MyInlineFunction(){ return…
2
votes
1 answer

Duplicate symbols error when including file that includes stb_image.h

In my main.h file, I #include "skybox.h". In skybox.h, stb_image.h is included (the latest version from GitHub as of 29-1-2017). Unlike any other library I've encountered, before including stb_image.h the docs say to #define…
2
votes
1 answer

Hide implementation in header-only library

I've tried to use an implementation namespace, but when i 'using namespace' it, it pulls in the whole implementation namespace, rendering it useless. namespace library { namespace implementation { //implementation } using namespace…
G_glop
  • 63
  • 1
  • 8
2
votes
1 answer

How to get a header only library portably from version control using CMake?

For a typical C++ header only library located on, e.g., github, at: https://github.com/username/library_name, has a directory structure with an include/library_name folder like: include/library_name containing all the library sources. This is…
gnzlbg
  • 7,135
  • 5
  • 53
  • 106
2
votes
1 answer

Header-only library design - include directives

I'm creating an header-only C++11/14 library and I'm not sure on how I should handle #include directives between library files. Should I try to group as many #include directives as possible in the user-oriented module header file or should internal…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
1
vote
0 answers

Can I reduce the compile-time of my header-only library with a separate translation unit?

I am looking for a way to reduce compile-time for header-only libraries. If there are only headers, each translation unit must compile all functions it needs, hence there is a lot of duplicate work done in the compilation step. My idea is to move…
Fabian
  • 4,001
  • 4
  • 28
  • 59
1
vote
2 answers

Nix: Propagate dependency for build-time but NOT for run-time?

I am packaging the following derivations. A C++ library named amazing that has a header-only C++ dependency (nlohmann_json for this example). The library must be dynamic (shared ELF file). The amazing library requires the nlohmann_json dependency…
deadlock
  • 93
  • 1
  • 5
1
vote
1 answer

cmake: compile header-only library into a STATIC library

I'm using VulkanMemoryAllocation, which is a header only library. I want to compile it into a static library using cmake, but I end up with an empty - 8 bytes sized - library file, and a lot of undefined symbols when linking. Here is the relevant…
dorfen_
  • 75
  • 1
  • 7
1
vote
1 answer

Multiple definition error (collect2: error: ld returned 1 exit status)

I was using https://github.com/jdduke/three_cpp as a header-only mode but faced some issues while compiling the same with my project. The issue happens when I include the following matrix4.hpp header (only necessary part is included) in more than…
user7664179
1
vote
0 answers

Creating a header-only library that can be included via add_subdirectory

I'm creating a lightweight header-only library for coroutines in C++20 called Conduit. The structure of the library looks like this, and it has no dependencies other than the C++ standard…
Alecto Irene Perez
  • 10,321
  • 23
  • 46
1
vote
1 answer

How do I force dependents on my INTERFACE library to use C++11 or later?

I'm in the process of splitting a library of mine into a header-only lib and a compiled lib, so, for the first time, I'm trying to use CMake to "build", or rather expose, a header-only library. Reading this and the CMake documentation, I understand…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1
vote
2 answers

Visual Studio trying to open source file that is no longer in project (fatal error C1083)

The problem is quite simple, really. I had imgui included in my project and I'm no longer using it. However, VS still tries to open them every time I try to compile. I removed all includes and deleted the files from my project completely. There is…
Julk
  • 169
  • 7
1
vote
2 answers

CMake target to just compile a source file

I am writing a header-only library using C++17. I would like to include it in a "dummy" source file: #include "my/library.h" // EOF The purpose is to ensure the library properly includes all of its dependencies. I also want to run static analyzers…
Krzysiek Karbowiak
  • 1,655
  • 1
  • 9
  • 17
1
vote
2 answers

Splitting up large file in header-only library

I'm working on a code base of a header-only library. It contains this Polygon class which has the problem that it is rather large: About 8000 lines. I'm trying to break this up but have been running into trouble. A couple of constraints for this…
Ghostkeeper
  • 2,830
  • 1
  • 15
  • 27