r/cprogramming • u/side2k • Oct 01 '26
What are the good practices of managing small dependencies?
I'm new to C. I have a small SDL2 project. At some point I needed to load and draw image sprites and found nice library stb_image.h. Probably best way here would be to just download the stb_image.h file and commit it to the repo, but I thought it would be a nice exercise towards learning how to manage dependencies. Since C doesn't seem to have anything like cargo, PyPi or npm, I started to look for best practices on how to do that.
So, this is what I came up with so far, with a Makefile:
- dependency file as a target, with commit pinned as separate var
STB_IMAGE_COMMIT := 013ac3beddff3dbffafd5177e7972067cd2b5083
:
- making it prerequisite to the main build target:
:
This seems simple, reproducible and efficient to me. But I'm not sure it would scale well and also - maybe there are better approaches out there?
6
u/tstanisl Oct 01 '26
Just add a file to your repository.
It is single-header library and it is designed to be dropped into existing projects. It will save you a lot of troubles with any breaking changes and it will make your built deterministic or it will keep working with no internet or if the link somehow vanishes. Just remember to add a proper licensing notice.
2
u/ffd9k Oct 01 '26
You can do this, but I would just use Meson and its Wrap system instead of reinventing it.
You basically add a small .wrap file to your project that contains the git or package url of the dependency. Meson then downloads the dependency and builds it as a subproject.
You can also do this just as a fallback - normally you want to use the system-installed version of a library, but if it is not available, you build it as a subproject.
If the dependency does not use meson as its build system, you add a patch_directory to your project with a meson.build file which builds the library.
1
u/alkavan Oct 01 '26
Use CMake's FetchContent it worked well for multiple C and C++ projects of mine.