错误:'parent_window'未声明

时间:2014-06-16 17:39:43

标签: c gtk gtk3

我在使用C编译程序时遇到问题。我想用这段代码简单地运行文件选择器:

#include <gtk/gtk.h>

int main( int argc, char *argv[])
{
  GtkWidget *dialog;

  gtk_init(&argc, &argv);

  dialog = gtk_file_chooser_dialog_new ("Open File",parent_window,action,_("_Cancel"),GTK_RESPONSE_CANCEL,_("_Open"),GTK_RESPONSE_ACCEPT,NULL);                   

 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  {
    char *filename;
    GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
    filename = gtk_file_chooser_get_filename (chooser);
    open_file (filename);
    g_free (filename);
  }

  gtk_widget_destroy (dialog);

  gtk_main();

  return 0;
}

我使用该命令编译它:

gcc -o s5 s5.c `pkg-config --libs --cflags gtk+-3.0`

我得到了错误:

s5.c:10:39: error: ‘parent_window’ undeclared (first use in this function)
                                       parent_window,
                                       ^
s5.c:10:39: note: each undeclared identifier is reported only once for each function it appears in
s5.c:11:39: error: ‘action’ undeclared (first use in this function)
                                       action,
                                       ^
s5.c:16:39: warning: passing argument 4 of ‘gtk_file_chooser_dialog_new’ makes pointer from integer without a cast [enabled by default]
                                       NULL);
                                       ^
In file included from /usr/include/gtk-3.0/gtk/gtk.h:99:0,
                 from s5.c:1:
/usr/include/gtk-3.0/gtk/gtkfilechooserdialog.h:63:12: note: expected ‘const gchar *’ but argument is of type ‘int’
 GtkWidget *gtk_file_chooser_dialog_new              (const gchar          *title,
            ^

我试图用GTK + 2做同样的事情并且也有同样的错误; /

2 个答案:

答案 0 :(得分:3)

对于那些想要从gtk +示例运行文件选择的人来说,这就是答案:

#include <gtk/gtk.h>

int main( int argc, char *argv[]){

    //dialog variable
    GtkWidget *dialog;    

    gtk_init(&argc, &argv);

    //Define dialog
    dialog = gtk_file_chooser_dialog_new ("Open File",NULL,GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL);

    //If file was choosen                 
    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT){
        char *filename;
        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));   
        printf("Selected file: %s\n", filename);
    } else {
        gtk_widget_destroy (dialog);
        return 0;
    }


    gtk_main();  
}

要编译您需要使用该命令:

gcc -o s6 s6.c `pkg-config --libs --cflags gtk+-2.0`

答案 1 :(得分:0)

问题是一些变量是未声明的;您必须复制并粘贴某些示例中的代码,并忘记定义它们。要创建一个非常简单的GTK对话框示例,请尝试以下方法:

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
    GtkWidget *dialog;

    gtk_init(&argc, &argv);

    dialog = gtk_file_chooser_dialog_new("Open File",
            NULL, GTK_FILE_CHOOSER_ACTION_OPEN,
            "_Cancel", GTK_RESPONSE_CANCEL,
            "_Open", GTK_RESPONSE_ACCEPT,
            NULL);

    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
        char *filename;
        GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
        filename = gtk_file_chooser_get_filename(chooser);
        g_message("File selected: %s", filename);
        g_free(filename);
    }

    gtk_widget_destroy (dialog);

    gtk_main();
    return 0;
}

并按照你的建议进行编译:

gcc -o s5 s5.c `pkg-config --libs --cflags gtk+-3.0`