To create a header-only library, simply ensure that all of your code is implemented in header files, as follows:
#ifndef MY_LIBRARY
#define MY_LIBRARY
namespace library_name
{
int my_api() { return 42; }
}
#endif
The preceding example implements a simple library with a single function. The entire implementation of this library can be implemented in a single header file and included in our code as follows:
#include "my_library.h"
#include <iostream>
int main(void)
{
using namespace library_name;
std::cout << "The answer is: " << my_api() << ' ';
return 0;
}
Although creating header-only libraries seems simple enough, there are some issues that arise when attempting to create a header-only library that should be taken into account.