0
votes
#include <gtk/gtk.h>
#include <curl/curl.h>
#include <iostream>
#include <string>

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

void pop_class()
{
    CURL *curl;
    CURLcode res;
    std::string readBuffer;

    curl = curl_easy_init();

    if(curl) 
    {
        curl_easy_setopt(curl, CURLOPT_URL, "http://api.openweathermap.org/data/2.5/forecast id=2158867&appid=a4f247bfd153738d2cd1757224361972");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }

}

int main(int argc, char *argv[])
{
    GtkBuilder      *builder; 
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new_from_file("glade/window_main.glade");

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
    gtk_builder_connect_signals(builder, NULL);

    g_object_unref(builder);

    gtk_widget_show(window);                
    gtk_main();

    pop_class();

    return 0;
}

// called when window is closed
void on_window_main_destroy()
{
    gtk_main_quit();
}

this compiles correctly with

g++ -c -g -O0 -Wall -pthread -pipe src/main.cpp -lcurl `pkg-config --cflags --libs gtk+-3.0` -o main.o

and then this

g++ -o temp_app main.o -pthread `pkg-config --cflags --libs gtk+-3.0`
-export-dynamic

and on running I get the following warning -

Could not find signal handler 'on_window_main_destory'. Did you compile with -rdnamic?

1
Looking at destory it looks like you have a typo.David Buck
on_window_main_destory vs on_window_main_destroyMichi
Spelling correct - just my typing on this forum. Both glade and .cpp have the same spelling 'on_window_main_destroy'carlHarbinson
May I suggest that you edit your question so it accurately reflects your problem. The act of editing will also bring it to more people's attention.David Buck

1 Answers

0
votes

GTK Builder wasn't really designed to interface with signal callbacks in C++ or other languages; C++ functions are so-called name mangled, which means that the real name of a particular function is likely quite different from the name the function is given in the source code. Your on_window_main_destroy function may well actually have a name like: _Zxl65Abvon_window_main_destroyxxj, depending on the compiler in use. This mangling is done to encode the function's namespace, parameter types, and return values among other things, so that overloading can work -- i.e., so that you can have two or more different functions with what looks like the same name, but which accept or return different parameters.

As such I'd really suggest using the g_signal_connect function on window, like this:

g_signal_connect (window, "destroy", G_CALLBACK (on_window_main_destroy), NULL);

However, if you must reference the callback from a Builder file, then surround the callback function with an extern "C" { } block, as such:

extern "C"
{
    void on_window_main_destroy()
    {
        gtk_main_quit();
    }
}

...or to simplify things a little:

extern "C" void on_window_main_destroy()
{
    gtk_main_quit();
}

You may also be able to take a shortcut here by simply setting the callback in your Builder file to gtk_main_quit. By doing such, you can avoid creating a function of your own altogether.