Noxbru
Noxbru

Reputation: 472

Headers Vs. Static Libraries

I am planning to do some utility code for using in other projects, but I am doubting between making it a mostly-headers code, a static library with headers just exposing the interface or something in between. Most of the code will be simple functions that wrap other standard functions but there will be also some bigger functions.

I think I mostly understand the differences between the header approach and the static library approach:

For the headers:

For the static library:

I have been looking at some code and I have seen both approaches, but I don't know which will be better. Any thoughts?

Upvotes: 3

Views: 2061

Answers (4)

Roberto Santalla
Roberto Santalla

Reputation: 585

My usual approach is to put in the headers the signatures and documentation (brief explanation of what every function does) alone, and to compile the code into a static (or shared) library.

This way, you can publish the precompiled library and the headers together, which gives the clients (and even a forgetful you in the future) an easy way of checking what your functions do, without having to skip into the implementation.

That would be my advice. However, C language does not impose these kind of behaviour, it's all up to the programmer.

Upvotes: 0

Sorcrer
Sorcrer

Reputation: 1644

I prefer to write it as a static library rather than using headers.

  • If you use headers it'll be inlined in the code which makes the code size to be large (if you're using an embedded system this is a disadvantage)
  • More compilation time will be required to compile the whole inlined function
  • But inlining saves times for context swiching while calling a function but this will be only in the range of a few microseconds(consider the penalty of code size also while using header method)
  • Implementing some functions are difficult by header method and more error prone refer: https://gcc.gnu.org/onlinedocs/cpp/Macro-Pitfalls.html
  • All the standard libraries made by the component/development board manufactures are static library method

;

Upvotes: 1

acwaters
acwaters

Reputation: 586

It's often possible to write your headers such that macros can be used to conditionally include the entire library (for compiling in one unit) or only declarations (for linking against static/shared objects or compiling in separate units) at the user's option. Using a macro like this has the added benefit that, assuming separate library sources and/or objects exist, the choice can be deferred to just before compilation and controlled by a build tool. The obvious drawback to this is the utter mess it can make of your code and all the complexity and cognitive strain that comes with using macros for conditional compilation.

Whether any given option (header/header+source/static-lib/shared-lib/etc.) is appropriate, or if the above option is useful or even possible, depends on what exactly you're doing.

Upvotes: 1

Isaiah
Isaiah

Reputation: 680

Unless these functions are one or two-liners, I think it would be better to separate implementation from declaration (use a static library).

  1. You are wasting time if you have to recompile the same, stable code everytime you make a change to a file.

  2. Inline functions do not give a tremendous amount of speed increase unless you use them a lot. Furthermore, it is the compiler's discretion whether or not to inline functions declared inline.

Upvotes: 0

Related Questions