使用G ++编译器编译GTK +应用程序 [英] Compiling GTK+ Application with G++ compiler
问题描述
我使用GTK +(不是gtkmm)在C ++中编写应用程序,所以我需要使用g ++编译器编译。是否可以使用g ++编译器编译GTK +应用程序? GTK +和库是否与g ++编译器兼容?
I am writing an application in C++ with using GTK+ (not gtkmm) so I need to compile using g++ compiler. Is it possible to compile GTK+ applications with the g++ compiler? Are GTK+ and libraries compatible with g++ compiler?
我试图在类中嵌入GTK +函数调用,如下所示:
I am trying to embed GTK+ functions call in a class like follows:
#include <gtk/gtk.h>
class LoginWindow
{
public:
LoginWindow();
void on_window_destroy( GtkObject *object, gpointer user_data);
private:
GtkBuilder *builder;
GtkWidget *window;
};
LoginWindow::LoginWindow()
{
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "login_window.glade", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "login_window"));
gtk_builder_connect_signals (builder, NULL);
g_signal_connect( GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(on_window_destroy), NULL );
g_object_unref (G_OBJECT (builder));
gtk_widget_show (window);
}
void LoginWindow::on_window_destroy (GtkObject *object, gpointer user_data)
{
gtk_main_quit ();
}
int main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
LoginWindow loginWindow;
gtk_main ();
return 0;
}
我这样做吗?我正在得到编译错误:
Am I doing it right? I am getting compile error on line:
g_signal_connect( GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(on_window_destroy), NULL );
login_window.cpp: In constructor "LoginWindow::LoginWindow()":
login_window.cpp:27: error: invalid use of member (did you forget the "&" ?)
这样做的正确方法是什么?
What is the right way of doing it?
推荐答案
Gtk +是一个基于C的库,所以你不能给它类成员函数作为回调。或者将C风格函数定义为回调,即
Gtk+ is a C-based library, so you can't give it class-member-functions as callbacks. Either define C-style functions as callbacks i.e.
extern "C"
void on_window_destroy( GtkObject *object, gpointer user_data)
{
// do yer stuff.. keep in mind, there's no this-pointer here, it's not
// associated with an object
}
。注册信号时,您可能需要将LoginWindow对象本身添加为user_data,只需在输入方法时强制转换,
转发调用,例如
outside of any class declarations. When registering the signal, you might want to add the LoginWindow-object itself as user_data and simply cast it when entering the method and forward the call, like so
extern "C"
void on_window_destroy( GtkObject *object, gpointer user_data)
{
static_cast<LoginWindow*>(user_data)->on_window_destroy();
}
如果你想做它的c ++风格虽然,看看gtkmm,这是Gtk +的c ++包装,它会给你更多的OOP你的钱。在gtkmm中使用信号/回调函数有点麻烦,但它的类型安全,最终会为你节省很多头痛。
If you want to do it c++ style though, have a look at gtkmm, which is the c++ wrapper for Gtk+, and it'll give you a lot more OOP for your buck. It's a little more messy to work with signals/callbacks in gtkmm, but it's type safe and will save you a lot of headaches in the end.
这篇关于使用G ++编译器编译GTK +应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!