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?
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.
I prefer to write it as a static library rather than using headers.
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With