Hacker News new | ask | show | jobs
by kazinator 128 days ago
> And header-only is more efficient to include and build with than header+source.

Dispute.

This is C code. You can't just drop it in and build it; you have to write code to use it. You have to figure out the API to correctly use it. If memory is passed around by pointers, you have to understand the responsibilities: who allocates, who frees, who may touch what when.

In the first place, you have to decide whether to commit to that library that far; it might not be until you've done some exploratory programming with it that you want to scrap it and find another one.

The cost of adding two files versus one is practically nothing in consideration of the larger picture.

The separate header model is baked into the C mindset; the tooling readily supports it.

Many "header only" libraries are secretly two files in one. When you define a certain preprocessor symbol before including the file, you get the implementation bits. When you don't define that symbol, the header is a pure header.

That means you have to pick some existing .c file which will define the preprocessor symbol and include the heaader. That source file becomes a "surrogate" source file, taking the place of the source file it ought to have.