升级到 glib-2.44 后的奇怪警告

Strange warnings after the upgrade to glib-2.44

升级到 Glib-2.44 后,我在尝试编译我的软件时收到这些警告:

In file included from /usr/include/glib-2.0/gobject/gobject.h:24:0,
                 from /usr/include/glib-2.0/gobject/gbinding.h:29,
                 from /usr/include/glib-2.0/glib-object.h:23,
                 from /usr/include/glib-2.0/gio/gioenums.h:28,
                 from /usr/include/glib-2.0/gio/giotypes.h:28,
                 from /usr/include/glib-2.0/gio/gio.h:26,
                 from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
                 from /usr/include/gtk-3.0/gdk/gdk.h:32,
                 from /usr/include/gtk-3.0/gtk/gtk.h:30,
                 from src/get_file_size.c:1:
/usr/include/glib-2.0/gio/glistmodel.h: In function ‘G_LIST_MODEL’:
/usr/include/glib-2.0/gobject/gtype.h:2186:40: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
     ((ct*) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
                                        ^
/usr/include/glib-2.0/gobject/gtype.h:479:66: note: in expansion of macro ‘_G_TYPE_CIC’
 #define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type)    (_G_TYPE_CIC ((instance), (g_type), c_type))
                                                                  ^
/usr/include/glib-2.0/gobject/gtype.h:1565:12: note: in expansion of macro ‘G_TYPE_CHECK_INSTANCE_CAST’
     return G_TYPE_CHECK_INSTANCE_CAST (ptr, module_obj_name##_get_type (), ModuleObjName); }               \
            ^
/usr/include/glib-2.0/gio/glistmodel.h:36:1: note: in expansion of macro ‘G_DECLARE_INTERFACE’
 G_DECLARE_INTERFACE(GListModel, g_list_model, G, LIST_MODEL, GObject)
 ^
/usr/include/glib-2.0/gio/glistmodel.h: In function ‘G_IS_LIST_MODEL’:
/usr/include/glib-2.0/gobject/gtype.h:2200:27: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
   GTypeInstance *__inst = (GTypeInstance*) ip; GType __t = gt; gboolean __r; \
                           ^
/usr/include/glib-2.0/gobject/gtype.h:491:66: note: in expansion of macro ‘_G_TYPE_CIT’
 #define G_TYPE_CHECK_INSTANCE_TYPE(instance, g_type)            (_G_TYPE_CIT ((instance), (g_type)))
                                                                  ^
/usr/include/glib-2.0/gobject/gtype.h:1567:12: note: in expansion of macro ‘G_TYPE_CHECK_INSTANCE_TYPE’
     return G_TYPE_CHECK_INSTANCE_TYPE (ptr, module_obj_name##_get_type ()); }                              \
            ^
/usr/include/glib-2.0/gio/glistmodel.h:36:1: note: in expansion of macro ‘G_DECLARE_INTERFACE’
 G_DECLARE_INTERFACE(GListModel, g_list_model, G, LIST_MODEL, GObject)
 ^
/usr/include/glib-2.0/gio/glistmodel.h: In function ‘G_LIST_MODEL_GET_IFACE’:
/usr/include/glib-2.0/gobject/gtype.h:2196:72: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
 #define _G_TYPE_IGI(ip, gt, ct)         ((ct*) g_type_interface_peek (((GTypeInstance*) ip)->g_class, gt))
                                                                        ^
/usr/include/glib-2.0/gobject/gtype.h:533:66: note: in expansion of macro ‘_G_TYPE_IGI’
 #define G_TYPE_INSTANCE_GET_INTERFACE(instance, g_type, c_type) (_G_TYPE_IGI ((instance), (g_type), c_type))
                                                                  ^
/usr/include/glib-2.0/gobject/gtype.h:1569:12: note: in expansion of macro ‘G_TYPE_INSTANCE_GET_INTERFACE’
     return G_TYPE_INSTANCE_GET_INTERFACE (ptr, module_obj_name##_get_type (), ModuleObjName##Interface); } \
            ^
/usr/include/glib-2.0/gio/glistmodel.h:36:1: note: in expansion of macro ‘G_DECLARE_INTERFACE’
 G_DECLARE_INTERFACE(GListModel, g_list_model, G, LIST_MODEL, GObject)
 ^
/usr/include/glib-2.0/gio/gliststore.h: In function ‘G_LIST_STORE’:
/usr/include/glib-2.0/gobject/gtype.h:2186:40: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
     ((ct*) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
                                        ^
/usr/include/glib-2.0/gobject/gtype.h:479:66: note: in expansion of macro ‘_G_TYPE_CIC’
 #define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type)    (_G_TYPE_CIC ((instance), (g_type), c_type))
                                                                  ^
/usr/include/glib-2.0/gobject/gtype.h:1396:12: note: in expansion of macro ‘G_TYPE_CHECK_INSTANCE_CAST’
     return G_TYPE_CHECK_INSTANCE_CAST (ptr, module_obj_name##_get_type (), ModuleObjName); }             \
            ^
/usr/include/glib-2.0/gio/gliststore.h:36:1: note: in expansion of macro ‘G_DECLARE_FINAL_TYPE’
 G_DECLARE_FINAL_TYPE(GListStore, g_list_store, G, LIST_STORE, GObject)
 ^
/usr/include/glib-2.0/gio/gliststore.h: In function ‘G_IS_LIST_STORE’:
/usr/include/glib-2.0/gobject/gtype.h:2200:27: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
   GTypeInstance *__inst = (GTypeInstance*) ip; GType __t = gt; gboolean __r; \
                           ^
/usr/include/glib-2.0/gobject/gtype.h:491:66: note: in expansion of macro ‘_G_TYPE_CIT’
 #define G_TYPE_CHECK_INSTANCE_TYPE(instance, g_type)            (_G_TYPE_CIT ((instance), (g_type)))
                                                                  ^
/usr/include/glib-2.0/gobject/gtype.h:1398:12: note: in expansion of macro ‘G_TYPE_CHECK_INSTANCE_TYPE’
     return G_TYPE_CHECK_INSTANCE_TYPE (ptr, module_obj_name##_get_type ()); }                            \
            ^
/usr/include/glib-2.0/gio/gliststore.h:36:1: note: in expansion of macro ‘G_DECLARE_FINAL_TYPE’
 G_DECLARE_FINAL_TYPE(GListStore, g_list_store, G, LIST_STORE, GObject)

这是一个函数的源代码(我也从其他不同的函数收到相同的警告,这是其中之一):

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

goffset
get_file_size (const gchar *file_path)
{
    GFileInfo *info;
    GFile *file;
    GError *error = NULL;
    const gchar *attributes = "standard::*";
    GFileQueryInfoFlags flags = G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS;
    GCancellable *cancellable = NULL;
    goffset file_size;

    file = g_file_new_for_path (file_path);
    info = g_file_query_info (file, attributes, flags, cancellable, &error);
    file_size = g_file_info_get_size (info);

    g_object_unref(file);

    return file_size;
}

我知道我可以使用 -Wno-cast-qual 避免它们,但我不想这样做。使用 Glib-2.42 我没有这些问题。
我无法理解如何解决此警告,因为我没有在特定代码段收到它。
有什么想法吗?

警告来自 GIO 本身内部 GListStoreGListModel 结构的重新定义,而不是来自您的代码。这是有效的 C11,以及最新版本的 GCC,但旧的编译器会发出警告。

This issue 最近已在 GLib 本身中修复,一旦发布,警告将在 GLib 2.44.1 中消失。