Stack Exchange
c++ gtk console log function is wrong?
Hello Community of excellent programmers and software engeneers ;)
I want to make a function which works like a log-function.
I have a c++ gtk GUI programm, where every buton-hit should be logged at the console, so if i click on "send" it schould write "send" into the console. Here the code:
#include <gtk/gtk.h>

static void printc(const gchar* msg, GtkWidget *widget, gpointer data){
  g_print(msg);
}

int main(int argc, char *argv[]){
  GtkBuilder *builder;
  GObject *wnd;
  GObject *btn;

  gtk_init(&argc, &argv);

  builder = gtk_builder_new();
  gtk_builder_add_from_file(builder, "builder.ui", NULL);

  wnd = gtk_builder_get_object(builder, "wnd");
  g_signal_connect(wnd, "destroy", G_CALLBACK(gtk_main_quit), NULL);

  btn = gtk_builder_get_object(builder, "btn_send");
  g_signal_connect(btn, "clicked", G_CALLBACK(printc("send")), NULL);

  gtk_main();

  return 0;
}

The error:
main.cpp:22:60: error: too few arguments to function ‘void printc(const gchar*, GtkWidget*, gpointer)’

g_signal_connect(btn, "clicked", G_CALLBACK(printc("send")), NULL);
How can I solve my problem?

The problem is you are attempting to call printc() instead of pass it as a reference used for the callback. This needs to be setup in a similar way to how the "destroy" signal is being used. Also note that signals and their callbacks accept "user data" as the tail argument which is what you can use for passing the custom per-callback text (you can also use g_signal_connect_swapped() if you want the text as the first parameter). Given this, setup the callback as follows:
static void printc (GtkWidget *widget, const gchar* msg) { ... }
...
g_signal_connect (btn, "clicked", G_CALLBACK(printc), (const gpointer)"send");

Or with connect swapped:
static void printc (const gchar* msg, GtkWidget *widget) { ... }
...
g_signal_connect_swapped (btn, "clicked", G_CALLBACK(printc), (const gpointer)"send");


Hm, don't work, here the compile error: http://pastebin.com/W2G13hnY and here the code: http://pastebin.com/6URfDakC

@Tekkzz: ah yes, for g++ you need to cast those strings to void pointers. I've updated the example.
