如何使用 MSYS2 和 Mingw-W64 让 GTK+ 3.0 信号处理程序在 Windows 10 上工作?

How do I make GTK+ 3.0 signal handler work on Windows 10 using MSYS2 and Mingw-W64?

这是我的申请代码 part1.c:

#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <gtk/gtkx.h>
#include <gtk/gtk.h>
#include <math.h>
#include <ctype.h>

GtkWidget *window;
GtkWidget *fixed1;
GtkWidget *button1;
GtkWidget *label1;
GtkBuilder *builder;

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);

    builder = gtk_builder_new_from_file("part1.glade");

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));

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

    gtk_builder_connect_signals(builder, NULL);

    fixed1 = GTK_WIDGET(gtk_builder_get_object(builder, "fixed1"));
    button1 = GTK_WIDGET(gtk_builder_get_object(builder, "button1"));
    label1 = GTK_WIDGET(gtk_builder_get_object(builder, "label1"));

    gtk_widget_show(window);

    gtk_main();

    return EXIT_SUCCESS;
}

void on_button1_clicked(GtkButton *b)
{
    gtk_label_set_text(GTK_LABEL(label1), (const gchar*)"Hello World!");
}

这是 part1.glade 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.0"/>
  <object class="GtkWindow" id="window">
    <property name="can_focus">False</property>
    <property name="resizable">False</property>
    <property name="default_width">640</property>
    <property name="default_height">480</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkFixed" id="fixed1">
        <property name="width_request">640</property>
        <property name="height_request">480</property>
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="width_request">190</property>
            <property name="height_request">20</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">label</property>
          </object>
          <packing>
            <property name="x">7</property>
            <property name="y">8</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="width_request">100</property>
            <property name="height_request">20</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_button1_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">136</property>
            <property name="y">6</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

要在 Windows 10 上编译程序,我使用 MSYS2 和 Mingw-W64: 我输入 gcc `pkg-config --cflags gtk+-3.0` part1.c -o part1.exe `pkg-config --libs gtk+-3.0` -lm 选项 -rdynamic 在 Windows 上不可用,我遇到的问题是当我单击按钮时没有任何反应。 MSYS2 上显示以下错误:

(part1.exe:608): Gtk-WARNING **: 11:26:19.469: Could not find signal handler 'on_button1_clicked'.  Did you compile with -rdynamic?

我应该怎么做才能使 on_button1_clicked 信号处理程序在 Windows 10 上工作?当使用 -rdynamic 编译时,相同的代码在 Linux 上运行没有缺陷,但此选项在 Windows 上不可用。我应该在这里做什么?

这里的技巧是将函数 void on_button1_clicked(GtkButton *b) 声明为 G_MODULE_EXPORT void on_button1_clicked(GtkButton *b) 这应该可以解决问题![​​=13=]

信用:http://blog.latepaul.com/glade-gtk-on-windows-with-msys2