使用g_rename func编译GTK + app时出错

时间:2010-02-04 00:01:59

标签: c linux ubuntu gtk

我正在尝试使用g_rename,编译器为代码中的行提供了奇怪的消息error: called object ‘rename’ is not a function

if (g_rename(old_file_name, full_new_file_name) == -1)

我不明白为什么。

我正在使用命令

进行编译
cc -Wall -g `pkg-config --cflags --libs gtk+-2.0` app.c -o app

Gcc version = gcc(Ubuntu 4.4.1-4ubuntu8)4.4.1

Gtk + version = 2.18.3-1ubuntu


以下是完整代码:

#include <gtk/gtk.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>

void
destroy (GtkWidget*, gpointer);
void
file_chooser_file_set(GtkWidget*, gpointer);

void
rename_clicked(GtkWidget*, gpointer);

int main (int argc, char* argv[]){
  GtkWidget *window, *vbox, *hbox, *rename, *text_rename, *file_chooser;

  gtk_init (&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "File renamer");
  g_signal_connect(G_OBJECT(window),
                    "destroy",
                    G_CALLBACK(destroy),
                    NULL);

  vbox = gtk_vbox_new(FALSE, 5);

  file_chooser = gtk_file_chooser_button_new("Choose file to rename:", GTK_FILE_CHOOSER_ACTION_OPEN);
  g_signal_connect(G_OBJECT(file_chooser),
                    "file-set",
                    G_CALLBACK(file_chooser_file_set),
                    NULL);

  gtk_box_pack_start(GTK_BOX(vbox),
                      GTK_WIDGET(file_chooser),
                      TRUE,
                      FALSE,
                      5);

  hbox = gtk_hbox_new(FALSE, 5);

  rename = gtk_button_new_with_label("Rename");

  gtk_box_pack_end(GTK_BOX(hbox),
                      GTK_WIDGET(rename),
                      FALSE,
                      FALSE,
                      5);

  text_rename = gtk_entry_new();
  g_signal_connect(G_OBJECT(rename),
                    "clicked",
                    G_CALLBACK(rename_clicked),
                    text_rename);
  gtk_box_pack_start(GTK_BOX(hbox),
                      GTK_WIDGET(text_rename),
                      TRUE,
                      TRUE,
                      5);

  gtk_box_pack_start(GTK_BOX(vbox),
                      GTK_WIDGET(hbox),
                      FALSE,
                      FALSE,
                      5);

  gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(vbox));
  gtk_widget_show_all(GTK_WIDGET(window));

  g_object_set_data(G_OBJECT(file_chooser),
                    "text_rename",
                    (gpointer) text_rename);
  g_object_set_data(G_OBJECT(file_chooser),
                    "rename",
                    (gpointer) rename);

  g_object_set_data(G_OBJECT(rename),
                    "file_chooser",
                    (gpointer)file_chooser);
  gtk_main();

  return 0;
}

#define _CHECK_FATAL_ERROR( e )\
  if ( (e) ) { \
    g_error("Fatal error: %s", (e)->message); \
    g_error_free( (e) );\
  }

#define _PATH_BUFFER_LENGTH 200
void
rename_clicked(GtkWidget* rename, gpointer text_entry) {
  gchar *new_file_name;
  gchar *old_file_name;
  gchar *full_new_file_name = g_new0(gchar, _PATH_BUFFER_LENGTH);
  GtkFileChooserButton *file_chooser;

  GError *error = NULL;

  GRegex *remove_file_name;

  remove_file_name = g_regex_new("/[^/]+$",
                                0,
                                0,
                                &error);
  _CHECK_FATAL_ERROR( error );


  file_chooser = GTK_FILE_CHOOSER_BUTTON(g_object_get_data(G_OBJECT(rename), "file_chooser"));

  new_file_name = g_strdup( gtk_entry_get_text(GTK_ENTRY(text_entry)) );
  old_file_name = g_strdup(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser)));
  if ( !( new_file_name && old_file_name ) )
    g_error("Error while allocating memory!");

  /*BEGIN*/
  /* full_new_file_name = dirname( old_file_name ) . new_file_name */
    gchar *path;

    path = g_regex_replace(remove_file_name,
                            old_file_name,
                            strlen(old_file_name),
                            0,
                            "/",
                            0,
                            &error);
    _CHECK_FATAL_ERROR( error );

    //Path
    full_new_file_name[0] = 0;
    g_strlcat(full_new_file_name, path, _PATH_BUFFER_LENGTH);
    //name
    g_strlcat(full_new_file_name, new_file_name, _PATH_BUFFER_LENGTH);
//    g_print("Full file name: %s\n", full_new_file_name);
    g_free( path );
  /*END*/

  if (g_rename(old_file_name, full_new_file_name) == -1)
    g_error("Error while renaming %s to %s", old_file_name, full_new_file_name);

  gtk_widget_set_sensitive(GTK_WIDGET(rename), FALSE);
  gtk_widget_set_sensitive(GTK_WIDGET(text_entry), FALSE);

  g_free( new_file_name );
  g_free( full_new_file_name );
  g_free( old_file_name );
  g_regex_unref( remove_file_name );
}

void
file_chooser_file_set(GtkWidget* file_chooser, gpointer null) {
  GtkButton *rename;
  GtkEntry  *text_rename;

  rename      = GTK_BUTTON(g_object_get_data(G_OBJECT(file_chooser),
                                             "rename") );
  text_rename = GTK_ENTRY(g_object_get_data(G_OBJECT(file_chooser),
                                            "text_rename"));

  gtk_widget_set_sensitive(GTK_WIDGET(rename), TRUE);
  gtk_widget_set_sensitive(GTK_WIDGET(text_rename), TRUE);
}

void destroy (GtkWidget* widget, gpointer data) {
  gtk_main_quit();
}

2 个答案:

答案 0 :(得分:3)

首先,您需要在您的计算机上安装libgtk2.0-dev和glibc-dev。 在Unix系统上,g_rename默认为C库函数rename的别名。 您需要#include <stdio.h>才能使用重命名功能而不会出现该错误。你看到的是gstdio.h头文件中的一个错误,它应该包含stdio.h。

答案 1 :(得分:0)

我没有安装glibc-dev包。之后一切正常,但它抱怨消息:

e4.1.c: In function ‘rename_clicked’:
e4.1.c:138: warning: implicit declaration of function ‘g_rename’