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

7

32

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:

  1. If the user wants to compile the library and link it dynamically, he/she can't.
  2. Changing a single line of code requires full recompilation of existing projects that depend on the library.

I really enjoy the aspects of header-only libraries though: all functions get potentially inlined and they're very very easy to include in your projects - no need to compile/link anything, just a simple #include directive.

Is it possible to get the best of both worlds? I mean - allowing the user to choose how he/she wants to use the library. It would also speed up development, as I'd work on the library in "dynamically-linking mode" to avoid absurd compilation times, and release my finished products in "header-only mode" to maximize performance.

The first logical step is dividing interface and implementation in .hpp and .inl files.

I'm not sure how to go forward, though. I've seen many libraries prepend LIBRARY_API macros to their function/class declarations - maybe something similar would be needed to allow the user to choose?


All of my library functions are prefixed with the inline keyword, to avoid "multiple definition of..." errors. I assume the keyword would be replaced by a LIBRARY_INLINE macro in the .inl files? The macro would resolve to inline for "header-only mode", and to nothing for the "dynamically-linking mode".

Dredge answered 1/9, 2014 at 13:15 Comment(3)
What kind of libraries are you talking about? A container library is often a set of templates, and these have to be "header-only". An applicative library is different.Gilkey
@BasileStarynkevitch: As an example, this is one of my header-only libraries. It is mostly template-based, but there are also modules that do not rely on templates (for example, the CommandLine module). Changing a single line during development in one of the non-template modules requires full recompilation. Also, the fact that interface and implementation are not separated bothers me. Do you think this kind of library is a candidate for my idea described in the question?Dredge
Your #1 point is kind of meaningless. If the library is header-only, I can't link it, true. (Unless I write my own wrapper library around it). And if you make it a dynamic library, I can't just #include it. It cuts both ways.Balsamic
D
19

Preliminary note: I am assuming a Windows environment, but this should be easily transferable to other environments.

Your library has to be prepared for four situations:

  1. Used as header-only library
  2. Used as static library
  3. Used as dynamic library (functions are imported)
  4. Built as dynamic library (functions are exported)

So let's make up four preprocessor defines for those cases: INLINE_LIBRARY, STATIC_LIBRARY, IMPORT_LIBRARY, and EXPORT_LIBRARY (it is just an example; you may want to use some sophisticated naming scheme). The user has to define one of them, depending on what he/she wants.

Then you can write your headers like this:

// foo.hpp

#if defined(INLINE_LIBRARY)
#define LIBRARY_API inline
#elif defined(STATIC_LIBRARY)
#define LIBRARY_API
#elif defined(EXPORT_LIBRARY)
#define LIBRARY_API __declspec(dllexport)
#elif defined(IMPORT_LIBRARY)
#define LIBRARY_API __declspec(dllimport)
#endif

LIBRARY_API void foo();

#ifdef INLINE_LIBRARY
#include "foo.cpp"
#endif

Your implementation file looks just like usual:

// foo.cpp

#include "foo.hpp"
#include <iostream>

void foo()
{
    std::cout << "foo";
}

If INLINE_LIBRARY is defined, the functions are declared inline and the implementation gets included like a .inl file.

If STATIC_LIBRARY is defined, the functions are declared without any specifier, and the user has to include the .cpp file into his/her build process.

If IMPORT_LIBRARY is defined, the functions are imported, and there isn't a need for any implementation.

If EXPORT_LIBRARY is defined, the functions are exported and the user has to compile those .cpp files.

Switching between static / import / export is a really common thing, but I'm not sure if adding header-only to the equation is a good thing. Normally, there are good reasons for defining something inline or not to do so.

Personally, I like to put everything into .cpp files unless it really has to be inlined (like templates) or it makes sense performance-wise (very small functions, usually one-liners). This reduces both compile time and - way more important - dependencies.

But if I choose to define something inline, I always put it in separate .inl files, just to keep the header files clean and easy to understand.

Desperado answered 5/9, 2014 at 9:56 Comment(3)
This seems a very good and clean approach. Some minor questions before accepting it: do you think there is any drawback using this method? Should I call the implementation files .inl or .cpp? (This is a mostly subjective question, but what do you think? Maybe template implementation in .inl and source in .cpp?)Dredge
@Vittorio - Like i wrote, it's the common approach for switching between static and dynamic linking. I never used it for conditional inlining, though. At first glance, i don't see any drawbacks in the mechanism itself, but maybe it tempts one to inline stuff that shouldn't be inlined and vice versa.Desperado
Regarding the naming: .cpp could cause some confusion if it gets included, but i would reserve .inl for stuff that gets included unconditionally (like template definitions).Desperado
G
5

It is operating system and compiler specific. On Linux with a very recent GCC compiler (version 4.9) you might produce a static library using interprocedural linktime optimization.

This means that you build your library with g++ -O2 -flto both at compile and at library link time, and that you use your library with g++ -O2 -flto both at compile and link time of the invoking program.

Gilkey answered 1/9, 2014 at 13:32 Comment(0)
T
2

Rationale

Put as little as necessary in header files and as much as possible in library modules, because of the very reasons that you mentioned: compile-time dependency and long compilation time. The only good reasons for header-only modules are:

  1. generic templates for user-defined template parameter;

  2. very short convenience functions when inlining gives significant performance.

In case 1, it is often possible to hide some functionality that does not depend on the user-defined type in a .cpp file.

Conclusion

If you stick to this rationale, then there is no choice: templated functionality that must allow user-defined types cannot be pre-compiled, but requires a header-only implementation. Other functionality should be hidden from the user in a library to avoid exposing them to the implementation details.

Tetragon answered 6/9, 2014 at 9:49 Comment(0)
T
2

This is to complement @Horstling's answer.


You can either create a static or a dynamic library. When you create statically-linked libraries, compiled code for all functions/objects will be saved to a file (with .lib extension in Windows). At main project (the project using the library) 's link time, these codes will be linked into your final executable together with the main project codes. So the final executable wouldn't have any runtime dependency.

Dynamically linked libraries will be merged into the main project at run time (and not link time). When you compile the library you get a .dll file (which contains actual compiled code) and a .lib file (which contains enough data for the compiler/runtime to find functions/objects in the .dll file). At link time, the executable will be configured to load the .dll and use compiled code from that .dll as needed. You will need to distribute the .dll file with your executable to be able to run it.

There is no need to choose between static or dynamic linking (or header-only) when designing your library, you create multiple project/makefiles, one to create a static .lib, another to create a .lib/.dll pair, and distribute both versions, for the user to choose between. (You'll need to use preprocessor macros like the ones @Horstling suggested).


You cannot put any templates in a pre-compiled library, unless you use a technique called Explicit Instantiation, which limits template parameters.

Also note that modern compiler/linkers usually do not respect the inline modifier. They may inline a function even if it's not designated as inline, or may dynamically call another that has inline modifier, as they see fit. (Regardless, I'll advise explicitly putting inline where applicable for maximum compatibility). So, there won't be any runtime performance penalty if you use a statically linked library instead of a header-only library (and enable compiler/linker optimizations, of course). As others have suggested, for really small functions that are sure to benefit from being called inline, it is best practice to put them in header files, so dynamically linked libraries will also not suffer any significant performance loss. (In any case, inlining functions will only affect performance for functions that are being called very often, inside loops that are going to be called thousands/millions of times).


Instead of putting inline functions in header files (with an #include "foo.cpp" in your header), you can change makefile/project settings and add foo.cpp to the list of source files to be compiled. This way, if you change any function implementation there will be no need to re-compile the whole project and only foo.cpp will be re-compiled. As I mentioned earlier, your small functions will still be inlined by the optimizing compiler, and you don't need to worry about that.


If you use/design a pre-compiled library, you should consider the case where the library is compiled with a different version of compiler to the main project. Each different compiler version (even different configurations, like Debug or Release) uses a different C runtime (things like memcpy, printf, fopen, ...) and C++ standard library runtime (things like std::vector<>, std::string, ...). These different library implementations may complicate linking, or even create runtime errors.

As a general rule, always avoid sharing compiler runtime objects (data structures that are not defined by standards, like FILE*) across libraries, because incompatible data structures will lead to runtime errors.

When linking your project, C/C++ runtime functions must be linked into your library .lib or .lib/.dll, or your executable .exe. C/C++ runtime itself can be linked as static or dynamic library (you can set this in makefile/project settings).

You will find that dynamically linking to C/C++ runtime in both the library and the main project (even when you compile the library itself as a static library) avoids most linking problems (with duplicate function implementations in multiple runtime versions). Of course you would need to distribute runtime DLLs for all used versions with your executable and library.

There are scenarios that statically linking to C/C++ runtime is needed, and the best approach in these cases would be to compile the library with the same compiler setting as the main project to avoid linking problems.

Thuggee answered 9/9, 2014 at 3:45 Comment(1)
You can get more flexibility on the point of putting template bodies in a prebuild library. First, you can explicitly instantiate to put ones you know are needed into a shared library, and prevent them from being instantiated for those types but know to link to them, when code uses the template. Second, you can put function bodies in a different .h file, and only include that file if you need a type that's not pre-compiled. This is the old portable way to prevent instantiation. It can prevent dragging in more dependencies in the (delcaration only) header.Bellman
C
1

Rather than a dynamic library, you could have a precompiled static library and thin header file. In an interactive quick build, you get the benefit of not having to recompile the world if implementation details changes. But a fully optimized release build can do global optimization and still figure out it can inline functions. Basically, with "link-time code generation" the toolset does the trick you were thinking about.

I'm familiar with Microsoft's compiler, which I know for sure does this as of Visual Studio 2010 (if not earlier).

Cassation answered 8/9, 2014 at 19:20 Comment(0)
I
1

Templated code will necessarily be header-only: for instantiating this code, the type parameters must be known at compilation time. There is no way to embed template code in shared libraries. Only .NET and Java support JIT instantiation from byte-code.

Re: non-template code, for short one-liners I suggest keeping it header-only. Inline functions give the compiler a lot more opportunities to optimize the final code.

To avoid "insane compilation time", Microsoft Visual C++ has a "precompiled headers" feature. I do not think GCC has a similar feature.

Long functions should not be inlined in any case.

I had one project which had header-only bits, compiled library bits and some bits I could not decide where belonged. I ended up having .inc files, conditionally included in either .hpp or .cxx depending on #ifdef. Truth to be told, the project was always compiled in "max inline" mode, so after a while I got rid of the .inc files and simply moved the contents to .hpp files.

Improvised answered 8/9, 2014 at 19:24 Comment(1)
GCC has PCHs since 3.4 (2006).Hun
H
0

Is it possible to get the best of both worlds?

In terms; limitations arise because tools aren't smart enough. This answer gives the current best effort that is still portable enough to be used effectively.

I've recently started thinking that this kind of design is not very good.

It ought to be. Header-only libraries are ideal because they simplify deployment: makes the reusing mechanism of the language similar to almost all others', which is just the sane thing to do. But this is C++. Current C++ tools still rely on half-a-century-old linking models that remove important degrees of flexibility, such as choosing which entry points to import or export on an individual level without being forced to change the library's original source code. Also, C++ lacks a proper module system and still relies on glorified copy-paste operations to work (although this is just a side factor to the problem in question).

In fact, MSVC is a little better in this regard. It is the only major implementation trying to achieve some degree of modularity in C++ (by attempting e.g. C++ modules). And it is the only compiler that actually allows e.g. the following:

//// Module.c++
#pragma once
inline void Func() { /* ... */ }

//// Program1.c++
#include <Module.c++>
// Inlines or "vague" links Func(), whatever is better.
int main() { Func(); }

//// Program2.c++
// This forces Func() to be imported.
// The declaration must come *BEFORE* the definition.
__declspec(dllimport) __declspec(noinline) void Func();
#include <Module.c++>
int main() { Func(); }

//// Program3.c++
// This forces Func() to be exported.
__declspec(dllexport) __declspec(noinline) void Func();
#include <Module.c++>

Note that this can be used to selectively import and export individual symbols from the library, although still cumbersomely.

GCC also accepts this (but the order of the declarations must be changed) and Clang does not have any way to achieve the same effect without changing the library's source.

Hun answered 28/12, 2016 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.