3 ms·
Show HN: Cmods – a header-only C modules library
- haneefmubarak 12y agoRecently, 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 12y agoThis 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 12y agoMainly, 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 12y agoI 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 12y agoThat'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.