0

I am porting some C# code (classes mostly) to C. I have analysed the code and the port is definitely possible. One thing I am not sure of though, is how to handle (i.e. implement) raising events in C.

I am in a Linux environment, and I expect the binary to run on Linux only. I heard that GLib supports events, but don't know much about this - any help would be appreciated.

A hello world example of raising an event and handling it in C would be very useful.

Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • It would be nice to not assume that folks who want to answer this *also* know C# well enough to understand what you mean by "events". So, could you please clarify what events are, and what they are used for? – unwind Nov 21 '12 at 10:24

1 Answers1

2

The closes thing to C# events implemented by GLib are the GObject signals.

Use g_signal_connect to connect your callback to an existing signal, and g_signal_emit to emit a registered signal. See the documentation for details.

As you are coming from a C# background, you might also consider using Vala, a programming language built around the GObject object system that compiles to C and has a feel similar to that of C# or Java.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • Actually, I'm coming from a C/C++ background :). I thought about using Vala previously, since I love the C# like syntax (without having to worry about the heavy .Net framework) - but alas the dev tools are not up to scratch and debugging the generated C code was an absolute nightmare ! – Homunculus Reticulli Nov 21 '12 at 13:24