Reputation: 472
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
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
Reputation: 1644
I prefer to write it as a static library rather than using headers.
;
Upvotes: 1
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
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).
You are wasting time if you have to recompile the same, stable code everytime you make a change to a file.
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