是一个简单的加法器程序!
main.c
代码: 全选
/*
* Initial main.c file generated by Glade. Edit as required.
* Glade will not overwrite this file.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
#include "interface.h"
#include "support.h"
int
main (int argc, char *argv[])
{
GtkWidget *window1;
#ifdef ENABLE_NLS
bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
#endif
gtk_set_locale ();
gtk_init (&argc, &argv);
add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");
/*
* The following code was added by Glade to create one of each component
* (except popup menus), just so that you see something after building
* the project. Delete any components that you don't want shown initially.
*/
window1 = create_window1 ();
gtk_widget_show (window1);
gtk_main ();
return 0;
}
interface.c
代码: 全选
/*
* DO NOT EDIT THIS FILE - it is generated by Glade.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
#define GLADE_HOOKUP_OBJECT(component,widget,name) \
g_object_set_data_full (G_OBJECT (component), name, \
gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)
#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
g_object_set_data (G_OBJECT (component), name, widget)
GtkWidget*
create_window1 (void)
{
GtkWidget *window1;
GtkWidget *table1;
GtkWidget *text1;
GtkWidget *text2;
GtkWidget *button;
GtkWidget *label;
window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window1), _("window1"));
table1 = gtk_table_new (4, 1, FALSE);
gtk_widget_show (table1);
gtk_container_add (GTK_CONTAINER (window1), table1);
text1 = gtk_entry_new ();
gtk_widget_show (text1);
gtk_table_attach (GTK_TABLE (table1), text1, 0, 1, 0, 1,
(GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
(GtkAttachOptions) (0), 20, 20);
gtk_entry_set_max_length (GTK_ENTRY (text1), 10);
gtk_entry_set_invisible_char (GTK_ENTRY (text1), 8226);
text2 = gtk_entry_new ();
gtk_widget_show (text2);
gtk_table_attach (GTK_TABLE (table1), text2, 0, 1, 1, 2,
(GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
(GtkAttachOptions) (0), 20, 20);
gtk_entry_set_invisible_char (GTK_ENTRY (text2), 8226);
button = gtk_button_new_with_mnemonic (_("ok"));
gtk_widget_show (button);
gtk_table_attach (GTK_TABLE (table1), button, 0, 1, 2, 3,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 50, 0);
label = gtk_label_new (_("result"));
gtk_widget_show (label);
gtk_table_attach (GTK_TABLE (table1), label, 0, 1, 3, 4,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 70, 30);
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_CENTER);
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
gtk_misc_set_padding (GTK_MISC (label), 100, 10);
g_signal_connect ((gpointer) button, "clicked",
G_CALLBACK (on_button_clicked),
NULL);
/* Store pointers to all widgets, for use by lookup_widget(). */
GLADE_HOOKUP_OBJECT_NO_REF (window1, window1, "window1");
GLADE_HOOKUP_OBJECT (window1, table1, "table1");
GLADE_HOOKUP_OBJECT (window1, text1, "text1");
GLADE_HOOKUP_OBJECT (window1, text2, "text2");
GLADE_HOOKUP_OBJECT (window1, button, "button");
GLADE_HOOKUP_OBJECT (window1, label, "label");
return window1;
}
callbacks.c
代码: 全选
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
void
on_button_clicked (GtkButton *button,
gpointer user_data)
{
GtkWidget *table1;
GtkWidget *text1;
GtkWidget *text2;
GtkWidget *label;
char a1[10];
char a2[10];
char a3[10];
float a,b,c;
strcpy(a1,gtk_entry_get_text(GTK_ENTRY(text1)));
strcpy(a2,gtk_entry_get_text(GTK_ENTRY(text2)));
a=atof(a1);
b=atof(a2);
c=a+b;
gcvt(c,7,a3);
gtk_label_set_text(GTK_LABEL(label),a3);
}
运行src/adder能出来图形界面,可是有错误提示make all-recursive
make[1]: 正在进入目录 `/home/chaoses/桌面/adder/adder'
Making all in src
make[2]: 正在进入目录 `/home/chaoses/桌面/adder/adder/src'
gcc -DHAVE_CONFIG_H -I. -I.. -DPACKAGE_DATA_DIR=\""/usr/local/share"\" -DPACKAGE_LOCALE_DIR=\""/usr/local/share/locale"\" -D_REENTRANT -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/directfb -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -DHAVE_CONFIG_H -I. -I.. -DPACKAGE_DATA_DIR=\""/usr/local/share"\" -DPACKAGE_LOCALE_DIR=\""/usr/local/share/locale"\" -D_REENTRANT -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/directfb -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -O2 -MT support.o -MD -MP -MF .deps/support.Tpo -c -o support.o support.c
mv -f .deps/support.Tpo .deps/support.Po
gcc -DHAVE_CONFIG_H -I. -I.. -DPACKAGE_DATA_DIR=\""/usr/local/share"\" -DPACKAGE_LOCALE_DIR=\""/usr/local/share/locale"\" -D_REENTRANT -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/directfb -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -O2 -MT interface.o -MD -MP -MF .deps/interface.Tpo -c -o interface.o interface.c
mv -f .deps/interface.Tpo .deps/interface.Po
gcc -DHAVE_CONFIG_H -I. -I.. -DPACKAGE_DATA_DIR=\""/usr/local/share"\" -DPACKAGE_LOCALE_DIR=\""/usr/local/share/locale"\" -D_REENTRANT -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/directfb -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -O2 -MT callbacks.o -MD -MP -MF .deps/callbacks.Tpo -c -o callbacks.o callbacks.c
callbacks.c: 在函数‘on_button_clicked’中:
callbacks.c:25: 警告: 隐式声明与内建函数‘strcpy’不兼容
mv -f .deps/callbacks.Tpo .deps/callbacks.Po
gcc -g -O2 -o adder main.o support.o interface.o callbacks.o -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lgio-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lglib-2.0
make[2]:正在离开目录 `/home/chaoses/桌面/adder/adder/src'
Making all in po
make[2]: 正在进入目录 `/home/chaoses/桌面/adder/adder/po'
make[2]: 没有什么可以做的为 `all'。
make[2]:正在离开目录 `/home/chaoses/桌面/adder/adder/po'
make[2]: 正在进入目录 `/home/chaoses/桌面/adder/adder'
make[2]:正在离开目录 `/home/chaoses/桌面/adder/adder'
make[1]:正在离开目录 `/home/chaoses/桌面/adder/adder'
然后点击OK按钮后图形界面直接关闭。(adder:1920): Gtk-CRITICAL **: gtk_entry_get_text: assertion `GTK_IS_ENTRY (entry)' failed
段错误
我估计是callbacks.c这个文件里面有什么问题,可手头也没有相关的参考书
大家帮忙看看出什么问题了,谢谢