简体   繁体   English

编译const GdkPixdata:“错误:初始化元素不是常量”

[英]Compiling const GdkPixdata: “error: initializer element is not constant”

I'm into a problem that I can't really understand. 我陷入了一个我无法理解的问题。
I want to "include" directly into my C code a series of icons. 我想直接在我的C代码中“包含”一系列图标。 Using the tool gdk-pixbuf-csource (with --struct option) I produced a file that initializes a 'const' pixdata structure for each image. 使用工具gdk-pixbuf-csource(带--struct选项)我生成了一个文件,为每个图像初始化一个'const'pixdata结构。 And example of an encoded image is this: 编码图像的例子如下:

const GdkPixdata pixdata_flipH = {
   0x47646b50, /* Pixbuf magic: 'GdkP' */
   24 + 479, /* header length + pixel_data length */
   0x2010002, /* pixdata_type */
   64, /* rowstride */
   16, /* width */
   16, /* height */
   /* pixel_data: */
   "\257\0\0\0\0\1\0\0\0\1\203\0\0\0\0\2\0\0\0\2\40J\207E\202\40J\207\377"
   "\202\0\0\0\0\202\40J\207\377\2\40J\207E\40J\207\2\202\0\0\0\0\1\0\0\0"
   "\2\203\0\0\0\0\4\40J\2072\40J\207\377\276\322\352\377\40J\207\377\202"
   <data continues......>
   "\0\0\0\0\1\40J\207\1\204\0\0\0\0\1\40J\207\3\227\0\0\0\0\1\0\0\0\3\244"
};

I want to include the icons in some buttons, so I prepared the following series of statements: 我想在一些按钮中包含图标,因此我准备了以下一系列语句:

GtkWidget *icon_flipH = gtk_image_new_from_pixbuf(gdk_pixbuf_from_pixdata(&pixdata_flipH, FALSE, NULL));
GtkWidget *icon_flipV = gtk_image_new_from_pixbuf(gdk_pixbuf_from_pixdata(&pixdata_flipV, FALSE, NULL)); 
// and so on...

In this way I'll add the image to the button with the function: 通过这种方式,我将使用以下功能将图像添加到按钮:

gtk_button_set_image(GTK_BUTTON(button_flipH), icon_flipH);

Well... The GCC compiler goes in error saying that "initializer element is not constant" at each line containing gdk_pixbuf_from_pixdata() function. 嗯...... GCC编译器在包含gdk_pixbuf_from_pixdata()函数的每一行中错误地说“初始化元素不是常量”。 I can't figure out why, because the function needs a const structure pointer and actually my structures are declared 'const'. 我无法弄清楚为什么,因为函数需要一个const结构指针,实际上我的结构被声明为'const'。

Am I forgetting something? 我忘记了什么吗? Is it not the right way to include images into the code and create pixbufs? 是不是将图像包含在代码中并创建pixbuf的正确方法? Any tutorial in this case? 在这种情况下的任何教程?

Objects defined at file scope (like icon_flipH ) have static storage duration: their lifetime is the lifetime of the program and they are initialized before the startup of the program. 在文件范围定义的对象(如icon_flipH )具有静态存储持续时间:它们的生命周期是程序的生命周期,它们在程序启动之前初始化。 You can only initialize objects with static storage duration with a constant expression (eg, 0 , 10 + 32 , or sizeof (int) ). 只能以恒定表达(例如,初始化具有静态存储持续时间的对象010 + 32 ,或sizeof (int) )。 The result of a function call is not a constant expression. 函数调用的结果不是常量表达式。

// File scope

GtkWidget *icon_flipH = NULL;   // OK, NULL is a constant expression  
GtkWidget *icon_flipV = foo();  // Not OK, foo is not a constant expression

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM