3

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:

  • All the code will be in headers
  • Better oportunities for the compiler to inline code
  • Every function will have to be compiled every time it is used

For the static library:

  • Mixed code between the library and the headers
  • Worse oportunities for the compiler to inline code
  • Compile once and forget

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

2
  • 1
    All the code will be in headers...am I reading it correct? Commented Aug 4, 2015 at 15:10
  • @SouravGhosh yes, you are reading it correctly. As an example of such: stb Commented Aug 4, 2015 at 15:49

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

;

Comments

0

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.