首页 > 解决方案 > 如何使用 MSYS2 和 Mingw-W64 使 GTK+ 3.0 信号处理程序在 Windows 10 上工作?

问题描述

这是我的应用程序的代码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 上工作?编译时相同的代码在 Linux 上运行没有缺陷,-rdynamic但此选项在 Windows 上不可用。我应该在这里做什么?

标签: cwindowsgtk3glademsys2

解决方案


这里的诀窍是将函数声明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


推荐阅读