Just because I'm curious--is there any C analog to the functionality of the STL in C++? I've seen mention of a GTK+ library called glib that a few people consider fills the bill but are there other libraries that would provide STL functionality in C?
- 1Why? 99% of the time you can convert well written C code to C++ code by just changing the compiler setting. Why not start using C++ code?davr– davr2008-10-14 15:42:24 +00:00Commented Oct 14, 2008 at 15:42
- @davr, I am using C++ code. I was just curious about the possibility of something like the STL (and its data structures) in C. I keep telling people using C++ to stop using arrays--use <vector> instead. So I was curious if there were something safer in C.Onorio Catenacci– Onorio Catenacci2008-10-14 17:08:26 +00:00Commented Oct 14, 2008 at 17:08
- 5@davr This question is quite useful for people who write code for embedded systems that only offer C compilers. If we can do C++ style programming in C, we can have C++ functionality on a huge variety of embedded processors.solvingPuzzles– solvingPuzzles2012-05-21 00:18:40 +00:00Commented May 21, 2012 at 0:18
4 Answers
Yes, glib is a pretty good choice: it includes a lot of utilities for manipulating containers like linked lists, arrays, hash tables, etc. And there is also an object-oriented framework called GObject that you can use to make objects with signals and slots in C (albeit with rather verbose function call names like gobject_set_property, since C doesn't have any syntax for objects). And there is also code for main loops so you can write event-driven programs.
For more info see wikipedia: https://en.wikipedia.org/wiki/GLib
Glib was originally part of GTK, but the non-GUI code has been completely factored out so that you can use it in command-line programs: http://library.gnome.org/devel/glib/stable/
Comments
Adding another option (full disclosure, I am the author); if you can compile and link C++, you can have a look into libcdada, which has a pure C API, but uses libstdc++ as a backend for most of the containers:
Comments
Well since STL's very nature is based on templates which C doesn't have, it would be difficult to even come close to the STL in C. The best you could hope for is some collection classes which manipulate void* pointers to unknown object.