

Show HN: Cmods – a header-only C modules library - haneefmubarak
https://github.com/haneefmubarak/cmods

======
haneefmubarak
Recently, after seeing a post here
([https://news.ycombinator.com/item?id=8401633](https://news.ycombinator.com/item?id=8401633))
about how to create a sort of modules in C, I thought it would be pretty cool
to wrap popular C libraries in this way with a header-only library.

This is what I made so far. So far, I have only wrapped _libc_ and _libm_ ,
but I'd love suggestions for other libraries to wrap too.

Other than that, what do you guys think?

~~~
readerrrr
This will likely prevent optimizations of intrinsic functions.

What is the benefit over simply including the necessary headers?

Other than the example you mentioned, I don't see much use for it.

~~~
haneefmubarak
Mainly, it keeps things slightly more organized and looks a bit nicer.
However, it also allows you to replace functions at runtime (although this is
generally dangerous).

All of this is done with essentially identical performance, at least on x86
and x64. (see the readme for details)

As for optimizations of inline functions, I don't include any functions marked
static inline, so those will automatically be inlined as before. Meanwhile,
link time optimization (LTO) can help with that too.

~~~
readerrrr
I don't see how can you have functions inlined while being able to replace
them at runtime.

Also, intrinsic functions might or might not get replaced with inline code,
depending on the compiler, thus losing performance.

~~~
haneefmubarak
That's a good point. I guess for those sensitive areas, you would just have to
use the functions as they are. However, as I mentioned, I don't include
`static inline` functions, so you will have to call those the normal way and
therefore will get all of the normal benefits such as inlining.

As for intrinsic functions, I'm familiar with compiler intrinsics, so if you
mean those, I don't include those either (well, I can't to be honest, since
they don't have symbols in a library). So those will still act as they always
have.

Meanwhile, for functions like memset() and such, my experimentation shows (at
least on GCC) that it only replaces the call with assembly for smaller values,
which I'm guessing are meant for use in tight loops. In places like that, it
may be valuable to just use the bare function name.

