2016-05-18 38 views
0

我是新來的C + + GTK +的GUI編程,我有一個問題,讓我的一個按鈕的信號處理程序工作,在編譯和運行時,我收到以下警告。有誰知道我是否做錯了什麼或缺少什麼?(C++)Gtk-WARNING **:找不到信號處理程序'。你用-rdynamic編譯過嗎?

(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) 

預先感謝任何幫助。

回答

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)。

順便說一句,如果你真的想用C++(不只是C)GTK,考慮gtkmm。但是你可以用簡單的C99代碼你的GTK程序(所以沒有cout,但是printf)。如果C++對你很重要,甚至可以考慮切換到Qt(C++中的另一個部件工具包)。

如果在C++中使用GTK編碼(不是gtkmm),最好聲明爲extern "C"全部您的GTK相關代碼。

+0

嘗試了不同的編譯代碼,它具有相同的結果。我試圖挽救Qt作爲最後的手段,但如果它更容易,我可以移動到gtkmm,如果它可以節省我的問題。 –

+0

也許'-rdynamic'應該先給出,*之前*'-o Project_Thoth';順便說一句,如果你只用C編寫代碼(包括你的東西,所以'printf',不要''cout <<'),編譯時間會更快,因爲C++代碼編譯速度很慢。 –

+0

是的,我可能只是用C只是因爲我知道那些作品。感謝您的幫助。 –