For headers only and only for static built-in library in C

I only write small headers and static

- inline

- only libraries in C. Would this be a bad idea when applied to large libraries? Or is it likely that runtimes will be faster with the headers-only version? Well, ignoring the obvious difference in compile times.

+3


source to share


2 answers


Yes, this is a bad idea - especially when integrated with larger libraries.

The problem of intrinsic complexity tends to increase as these libraries are included and visible for more translations and more complex header inclusion graphs, which is quite common in large projects. It becomes much more time consuming to build as the number of transfers and dependencies increase. Magnification is usually not linear complexity.

There are reasons why this flies in C ++ but not in C. the inline

export semantics are different. In short, you will end up producing many copies of functions in C variables (as well as functions). C ++ deduplicates them. C doesn't.



Also, inlining is not a silver bullet for speed. This approach often increases the size of your code and the size of the executable. Larger functions can generate slower code. Copies of programs / functions can also slow down your program. Large binaries take longer to link and initialize (= launch). Less is usually better.

Better to consider alternatives like link time optimization, whole program optimization, library design, using C ++ - and avoiding defining C in headers.

Also keep in mind that the compiler can remove dead code and the linker can remove unused functions.

+3


source


I wrote a single test framework * as one C89 header file. Essentially, it's all macro or tagged static and join time optimization (partially) deduplicates the result.

This is an ease-of-use benefit since integration with build systems is trivial.

Compile times are ok as it is C, but the resulting duplication function bothers me a little. So it can be used as header + source by setting the macro to #including in one source file eg.



#define MY_LIB_HEADER_IMPLEMENTATION
#include "my_lib.h"

      

I don't think I would use this approach for a larger project, but I think it is optimal for what is essentially a set of unit test macros.

  • in the section "do not call us, we will call you" meaning
0


source







All Articles