【发布时间】:2010-12-13 21:08:16
【问题描述】:
我有一个结构,其中定义了一个常量 char 字符串和一个指向我自己的字符串对象的指针。目标是使用 chars 集和 txt 集 t NULL 声明此结构的变量,然后在运行时创建代表 chars 的 MyString 对象。我在使用 GLib 时无法在编译期间创建 MyString,并且此库首先需要调用 g_type_init。
struct _MyStaticString {
volatile MyString * txt;
const char *chars;
};
声明将如下所示:
struct _MyStaticString my_test_string = { NULL, "Hello world or foo bar, or a rick roll" };
然后它们是一个函数,它可以通过首先检查 txt 是否为 NULL 来向我传递 MyString 对象,如果是,则创建一个新的 MyString 对象并返回这个 MyString 对象。
struct _MyString *my_static_string(struct _MyStaticString *a_static) {
printf("a_static=%lx\n", (gulong) a_static);
printf("a_static.chars=%s\n", (char *) a_static->chars);
if (a_static->txt == NULL) {
CatString *result = g_object_new(MY_TYPE_STRING, NULL);
// result->data = (gchar *) a_static->chars;
result->data = strdup((char *) a_static->chars);
result->size = strlen((char *) a_static->chars);
result->hash = 0;
g_object_ref_sink(G_OBJECT(result));
result->parent.ref_count = 1000;
a_static->txt = result;
}
return (struct _MyString *) (a_static->txt);
}
这一切都很好,我很高兴,至少当我在 Linux 上运行 GCC 时。当我在 MinGW 编译器的帮助下开始在 Windows 上编译这段代码时,事情就开始出错了。如果我将所有内容都放在一个项目中,它仍然可以,但只要将声明放入 .a 库并在其他地方使用它,字段 a_static->chars 就会变为 NULL。所以我开始玩/调整/测试:我想可能是对象文件中数据的对齐,因此添加了#pragma pack(16)。它没有用。比我想象的也许有一个属性可以帮助我。所以我添加了__attribute__ ((common))。它没有用。我认为很聪明,并将字符串与结构声明本身分开,例如:
const char helper_txt = "Hello world or foo bar, or a rick roll";
struct _MyStaticString my_test_string = { NULL, helper_txt };
我得到编译错误:
error: initializer element is not constant
error: (near initialization for 'field.chars')
这是我的编译器标志
C:\MinGW\bin\gcc.exe
-IC:\MinGW\include
-IC:\GTK_ALL_IN_ONE\include\gtk-2.0
-IC:\GTK_ALL_IN_ONE\lib\gtk-2.0\include
-IC:\GTK_ALL_IN_ONE\include\atk-1.0
-IC:\GTK_ALL_IN_ONE\include\cairo
-IC:\GTK_ALL_IN_ONE\include\gdk-pixbuf-2.0
-IC:\GTK_ALL_IN_ONE\include\pango-1.0
-IC:\GTK_ALL_IN_ONE\include\glib-2.0
-IC:\GTK_ALL_IN_ONE\lib\glib-2.0\include
-IC:\GTK_ALL_IN_ONE\include
-IC:\GTK_ALL_IN_ONE\include\freetype2
-IC:\GTK_ALL_IN_ONE\include\libpng14
-IC:\work\workspace\module-blah\src
-O0 -g3 -Wall -c -fmessage-length=0 -mms-bitfields -DOSWINDOWS
这是版本
C:\>c:\MinGW\bin\gcc.exe --version
gcc.exe (GCC) 4.5.0
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我是否遗漏了一些编译器标志,或者我是否需要添加一个属性来确保将 const char * 字符串导出到 .a 库中,并且它们是检查字符串是否在 .a 库中的一种简单方法?或者它可能是一些链接器选项?
【问题讨论】:
-
您正在使用保留标识符:C 中以下划线后跟大写字母开头的任何标识符都保留用于实现使用(参见标准的 7.1.3),因此该程序调用 undefined行为。我的规则是永远不要用下划线开始我的标识符。
-
另外,“字段 a_static->chars 变为 NULL”是什么意思?具体是怎么回事?是
const char helper_txt还是正确的const char * helper_txt?