这是一个简短的程序来演示这个问题:
void main(string[] args)
{
unichar c = 'a';
string str_from_to_string = c.to_string(); // Warning
stdout.printf("Converted by unichar.to_string: "%s"n", str_from_to_string);
}
这也会导致相同的警告:
void main(string[] args)
{
unichar c = 'a';
string str_from_template = @"$c"; // Warning
stdout.printf("Converted by string template: "%s"n", str_from_template);
}
这是我收到的警告:
/home/mpiroc/Desktop/unicode_to_string/unicode_to_string.vala.c: In function ‘g_unichar_to_string’:
/home/mpiroc/Desktop/unicode_to_string/unicode_to_string.vala.c:26:2: warning: passing argument 2 of ‘g_unichar_to_utf8’ discards ‘const’ qualifier from pointer target type [enabled by default]
/usr/include/glib-2.0/glib/gunicode.h:684:11: note: expected ‘gchar *’ but argument is of type ‘const gchar *’
这是警告中提到的生成的 c 代码:
18 static gchar* g_unichar_to_string (gunichar self) {
19 gchar* result = NULL;
20 gchar* _tmp0_ = NULL;
21 gchar* str;
22 const gchar* _tmp1_;
23 _tmp0_ = g_new0 (gchar, 7);
24 str = (gchar*) _tmp0_;
25 _tmp1_ = str;
26 g_unichar_to_utf8 (self, _tmp1_);
27 result = str;
28 return result;
29 }
似乎_tmp_
可能不应该标记为const
,但这是由valac
生成的,而不是我直接编写的。我做错了什么吗?或者这是valac
中的错误?代码按预期运行,但我尽量避免警告。
添加 --禁用警告命令行选项
IIRC Vala 编译器也可以使用 --disable-warnigs 命令行选项。
编辑:
gcc 命令行选项
抱歉,Valac 是转译器,所以一旦 valac --ccode 输出,然后需要运行 GCC 或其他编译器。
$ valac --ccode unicode_to_string.vala
$ gcc -o unicode_to_string -w unicode_to_string.c `pkg-config --libs --cflags gobject-2.0`
Vala 编译器不会使临时变量const
。您可以安全地忽略有关const
-ness的警告。