Headings vs. Static libraries

I am planning to make some utility code for use in other projects, but I doubt if it makes most of the headers code, a static library with headers, just by opening an interface or something in between. Most of the code will be simple functions that will wrap other standard functions, but there will be some larger functions as well.

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

For headers:

  • All code will be in headers
  • Improving compiler capabilities for inline code
  • Each function must be compiled every time it is used

For a static library:

  • Mixed code between library and headers
  • Worse compiler options for inline code
  • Compile once and forget

I've looked at some code and I've seen both approaches, but I don't know which would be better. Any thoughts?

+3


source to share


4 answers


It is often possible to write headers in such a way that macros can be used to conditionally include the entire library (for compiling in one device) or only declarations (for binding to static / shared objects or compiling in separate units) of the user's choice.Using such a macro has the added benefit which, assuming there are separate library sources and / or objects, can be delayed until compilation and management of the build tool. The obvious downside to this is the complete mess it can make of your code, as well as all the complexity and cognitive overhead that comes with using macros for conditional compilation.



Whether any given option (header / header + source / static-lib / shared-lib / etc.) Matches, or if this option is useful or even possible depends on what you are doing.

+1


source


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

  • If you are using headers it will be embedded in the code, which makes the code size large (if you are using an embedded system, this is a disadvantage).
  • It will take longer compilation time to compile the entire built-in function
  • But nesting saves time for context swiching when calling a function, but it will only be in the range of a few microseconds (consider code size penalty and when using header method)
  • Implementing some functionality is tricky with the header method, and more error links: https://gcc.gnu.org/onlinedocs/cpp/Macro-Pitfalls.html
  • All standard libraries made by component / design manufacturers are static library methods.


;

+1


source


If these functions are not one or two lines, I think it would be better to separate the implementation from the declaration (use a static library).

  • You waste time if you need to recompile the same stable code every time you change the file.

  • The built-in functions don't give a huge speed boost unless you use a lot of them. Also, it is at the discretion of the compiler whether or not inline functions are declared inline.

0


source


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

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

That would be my advice. However, the C language does not enforce this behavior, it all depends on the programmer.

0


source







All Articles