Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header-only linking

Tags:

c++

c

linker

Many C++ projects (e.g. many Boost libraries) are "header-only linked".

Is this possible also in plain C? How to put the source code into headers? Are there any sites about it?

like image 720
Cartesius00 Avatar asked Sep 28 '11 12:09

Cartesius00


People also ask

What is a header-only binding?

In the context of the C or C++ programming languages, 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.

Are header-only libraries good?

The benefit of header-only libraries is that they are easy to include in your project as you simply include the header and you are done (there is no need to compile the library as there are no source files to compile).

Why are some libraries header-only?

Having a header-only library also means you don't have to worry about different platforms where the library might be used. When you separate the implementation, you usually do so to hide implementation details, and distribute the library as a combination of headers and libraries ( lib , dll 's or . so files).

Why is it called a header file?

Header Files: The files that tell the compiler how to call some functionality (without knowing how the functionality actually works) are called header files. They contain the function prototypes. They also contain Data types and constants used with the libraries.


2 Answers

Executive summary: You can, but you shouldn't.

C and C++ code is preprocessed before it's compiled: all headers are "pasted" into the source files that include them, recursively. If you define a function in a header and it is included by two C files, you will end up with two copies in each object file (One Definition Rule violation).

You can create "header-only" C libraries if all your functions are marked as static, that is, not visible outside the translation unit. But it also means you will get a copy of all the static functions in each translation unit that includes the header file.

It is a bit different in C++: inline functions are not static, symbols emitted by the compiler are still visible by the linker, but the linker can discard duplicates, rather than giving up ("weak" symbols).

It's not idiomatic to write C code in the headers, unless it's based on macros (e.g. queue(3)). In C++, the main reason to keep code in the headers are templates, which may result in code instantiation for different template parameters, which is not applicable to C.

like image 157
Alex B Avatar answered Oct 11 '22 11:10

Alex B


You do not link headers.

In C++ it's slightly easier to write code that's already better-off in headers than in separately-compiled modules because templates require it. 1

But you can also use the inline keyword for functions, which exists in C as well as C++. 2

// Won't cause redefinition link errors, because of 6.7.4/5
inline void foo(void) {
   // ...
}

[c99: 6.7.4/5:] A function declared with an inline function specifier is an inline function. The function specifier may appear more than once; the behavior is the same as if it appeared only once. Making a function an inline function suggests that calls to the function be as fast as possible. The extent to which such suggestions are effective is implementation-defined.

You're a bit stuck when it comes to data objects, though.


1 - Sort of.
2 - C99 for sure. C89/C90 I'd have to check.

like image 20
Lightness Races in Orbit Avatar answered Oct 11 '22 09:10

Lightness Races in Orbit