gpt4 book ai didi

c++ - (C++) Gtk-WARNING ** : Could not find signal handler'. 是否使用 -rdynamic 进行编译?

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:38 31 4
gpt4 key购买 nike

我是使用 GTK+ 进行 C++ GUI 编程的新手,我在让其中一个按钮的信号处理程序正常工作时遇到问题,在编译和运行时我收到以下警告。有谁知道我是做错了什么还是遗漏了什么?

(Project Thoth:4359):Gtk-WARNING **:找不到信号处理程序“on_login_clicked”。你用 -rdynamic 编译了吗?

这是我的cpp文件

#include <cstdlib>
#include <cstdio>
#include <string>
#include <gtk/gtk.h>
#include <iostream>
extern "C"
using namespace std;

int main(int argc, char *argv[])
{
GtkEntry *e1;//textbox1
GtkEntry *e2;//textbox2
GtkEntry *e3;//textbox3

GtkBuilder *gtkBuilder;
GtkWidget *window;

gtk_init(&argc, &argv);

gtkBuilder = gtk_builder_new();
gtk_builder_add_from_file(gtkBuilder, "Unsaved 1.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "Login"));

gtk_builder_connect_signals(gtkBuilder, window);

g_object_unref(G_OBJECT(gtkBuilder));
gtk_widget_show(window);
gtk_main();
cout << "Login window loaded";
return 0;
}

G_MODULE_EXPORT void on_login_clicked()
{
printf("Request Detected\n");
GtkBuilder *gtkBuilder;
GtkWidget *window;

gtkBuilder = gtk_builder_new();
gtk_builder_add_from_file(gtkBuilder, "Unsaved 1.glade", NULL);

GtkEntry *e1;
GtkEntry *e2;
e1 = GTK_ENTRY( gtk_builder_get_object( gtkBuilder, "user1" ));
e2 = GTK_ENTRY( gtk_builder_get_object( gtkBuilder, "pass"));
gtk_builder_connect_signals(gtkBuilder, e1);
const char *home;
home = gtk_entry_get_text(e1);
cout << home;
}

还有我的林间空地文件

    <?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.0"/>
<object class="GtkApplicationWindow" id="Login">
<property name="can_focus">False</property>
<property name="window_position">center</property>
<property name="default_width">400</property>
<property name="default_height">300</property>
<signal name="destroy" handler="on_window_destroy" swapped="no"/>
<child>
<object class="GtkLayout" id="layout1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="Label">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Project Thoth
</property>
<property name="angle">0.089999999999999997</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="x">156</property>
<property name="y">14</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="user1">
<property name="width_request">166</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="x">128</property>
<property name="y">72</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label1">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Username:
</property>
</object>
<packing>
<property name="x">121</property>
<property name="y">52</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="pass">
<property name="width_request">166</property>
<property name="height_request">84</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="x">129</property>
<property name="y">130</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label3">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Password:

</property>
</object>
<packing>
<property name="x">117</property>
<property name="y">123</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Login</property>
<property name="width_request">70</property>
<property name="height_request">29</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="relief">half</property>
<signal name="clicked" handler="on_login_clicked" swapped="no"/>
</object>
<packing>
<property name="x">171</property>
<property name="y">191</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label2">
<property name="width_request">105</property>
<property name="height_request">35</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">save info login ?</property>
</object>
<packing>
<property name="x">154</property>
<property name="y">227</property>
</packing>
</child>
<child>
<object class="GtkSwitch" id="switch1">
<property name="width_request">74</property>
<property name="height_request">23</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="x">166</property>
<property name="y">263</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

我还使用了以下标志进行编译

g++ -o "Project Thoth" main.cpp $(pkg-config --cflags --libs gtk+-3.0 gmodule-2.0 )

在此先感谢您的帮助。

最佳答案

该消息给出了一个很好的建议:使用 -rdynamic 编译(实际上是链接),因此使用以下代码编译您的代码:

 g++ -rdynamic  -o Project_Thoth -Wall -g main.cpp \
$(pkg-config --cflags --libs gtk+-3.0 gmodule-2.0 )

顺便说一句,我强烈建议您避免在可执行文件名称中使用空格。所以 Project_Thoth 不是 “Project Thoth”。此外,不要忘记询问警告(至少使用 -Wall,也许还可以添加 -Wextra)和调试信息(-g),这非常有用,尤其是使用 gdb 调试器调试程序(或使用 valgrind 寻找内存泄漏)。

程序调试完成后,出于基准测试的目的,您可能会要求编译器进行优化(因此添加 -O2)。

顺便说一句,如果你真的想将 GTK 与 C++(不仅仅是 C)一起使用,请考虑 gtkmm .但是您可以使用纯 C99 编写您的 GTK 程序(因此没有 cout,而是 printf)。甚至可以考虑切换到 Qt (另一个小部件工具包,在 C++ 中),如果 C++ 对您很重要。

如果使用 GTK(不是 gtkmm)在 C++ 中编码,最好将您的 GTK 相关代码声明为 extern "C" 所有

关于c++ - (C++) Gtk-WARNING ** : Could not find signal handler'. 是否使用 -rdynamic 进行编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37305423/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com