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

时间:2019-08-30 15:37:11

标签: c windows gtk3 glade msys2

这是我的应用程序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上没有此选项。我应该在这里做什么?

1 个答案:

答案 0 :(得分:1)

这里的窍门是将函数void on_button1_clicked(GtkButton *b)声明为G_MODULE_EXPORT void on_button1_clicked(GtkButton *b) 这应该可以解决问题!

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