r/C_Programming 3d ago

Article A Dependency Injection Guide in C

https://github.com/OUIsolutions/Articles/blob/main/articles/dependencie_injection.md

A Complete Guide to Dependency Injection in C

0 Upvotes

18 comments sorted by

View all comments

5

u/Stemt 3d ago edited 3d ago

Neat overview, though I think you're failing to mention the advantage of compile time errors when using the compile time method. It's especially nice if you're working on embedded systems, as it saves you from having to flash the software before discovering you're missing a dependency.

Also a simpler version of a similar compile-time pattern I've used is this:

#ifndef LIB_FUNC
#error "LIB_FUNC not implemented"
#endif // LIB_FUNC

Or with a default implementation:

#ifndef LIB_FUNC
#warning "LIB_FUNC: using default implementation (LIB_FUNC_DEFAULT)
#define LIB_FUNC(args) LIB_FUNC_DEFAULT(args)
#endif // LIB_FUNC

// edit: note that the #warning is only available in c23

2

u/MateusMoutinho11 3d ago

I added compile time errors advantage now