本文介绍了理解 vala 编译警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的编译警告对我来说不是很清楚,从弃用战争,但 valadoc 中方法的签名:

http://valadoc.org/#!api=gstreamer-1.0/Gst>

没有显示其他方法签名.

其他警告更加模糊.

max@max-ubuntu:~/mdev/cr valac --pkg gstreamer-0.10 gstpipe.vala/home/max/dev/main-sandbox/cr/gstpipe.vala.c:在函数‘application_message’中:/home/max/dev/main-sandbox/cr/gstpipe.vala.c:64:2: 警告:传递_gst_structure_copy0"的参数 1 会丢弃指针目标类型的const"限定符 [默认启用]/home/max/dev/main-sandbox/cr/gstpipe.vala.c:26:17: 注意:应为gpointer",但参数类型为const struct GstStructure *"/home/max/dev/main-sandbox/cr/gstpipe.vala.c:82:9: 警告:赋值丢弃了来自指针目标类型的const"限定符 [默认启用]/home/max/dev/main-sandbox/cr/gstpipe.vala.c:在函数‘main’中:/home/max/dev/main-sandbox/cr/gstpipe.vala.c:173:2: 警告:'g_type_init' 已弃用(在/usr/include/glib-2.0/gobject/gtype.h:669 声明)[-Wdeprecated-声明]

使用 Gst;void application_message(Gst.Bus 总线,Gst.Message msg) {var s = msg.get_structure();if(s == null)返回;字符串 msgtype = s.get_name();if(msgtype != "level")返回;GLib.Value rms = s.get_value("rms");//GLib.Value st = s.get_value("stream-time");GLib.DateTime now = new GLib.DateTime.now_local();var sec = now.to_unix();var msec = (sec * 1000) + now.get_microsecond();var z = rms.strdup_contents();//z = z.replace("{", "[").replace("}", "]");stdout.printf("%ld, %s \n", (long) msec, z);}void main (string[] args) {Gst.init (ref args);尝试 {var 管道 = Gst.parse_launch("pulsesrc device=\"alsa_input.usb-046d_08c9_674634A4-02-U0x46d0x8c9.analog-mono\" !" +"级别名称=波级别间隔=10000000 !" +wavenc!filesink location=audioz.wav");var bus = pipeline.get_bus();bus.add_signal_watch();bus.message.connect(application_message);//将管道状态设置为 PLAYINGpipeline.set_state (State.PLAYING);//创建并启动 GLib 主循环new MainLoop().run();}捕获(错误 e){print("%s\n", e.message);}}

解决方案

在使用 Vala 时,您通常可以忽略来自 C 编译器的警告.Vala 拥有比 C 编译器更好的信息,因此当 C 编译器无法知道某些事情时,它知道某些事情是有效的.不幸的是,我们不能在任何地方都添加强制转换,因为有些情况下我们无法生成有效的强制转换(而且,更重要的是,无法知道这些情况是什么).

关于 g_type_init 被弃用的最后一个警告是因为从 glib 2.36 开始不再需要 g_type_init.您可以通过将 --target-glib=2.36(或任何更高版本的 glib)传递给 valac 来消除该警告,但请注意,生成的代码可能不再适用于旧版本的 glib.

TBH,我经常只是将 -X -w 传递给 valac 以使 C 编译器安静.有时我会错过一个有用的警告,但它消除了很多无用的警告.

The compilation warnings below are not so clear to me, appart from the deprecation warhing, but the signature of the method in the valadoc :

http://valadoc.org/#!api=gstreamer-1.0/Gst

shows no other method signature.

the other warning are more obscure.


max@max-ubuntu:~/mdev/cr valac --pkg gstreamer-0.10 gstpipe.vala 
/home/max/dev/main-sandbox/cr/gstpipe.vala.c: In function ‘application_message’:
/home/max/dev/main-sandbox/cr/gstpipe.vala.c:64:2: warning: passing argument 1 of ‘_gst_structure_copy0’ discards ‘const’ qualifier from pointer target type [enabled by default]
/home/max/dev/main-sandbox/cr/gstpipe.vala.c:26:17: note: expected ‘gpointer’ but argument is of type ‘const struct GstStructure *’
/home/max/dev/main-sandbox/cr/gstpipe.vala.c:82:9: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
/home/max/dev/main-sandbox/cr/gstpipe.vala.c: In function ‘main’:
/home/max/dev/main-sandbox/cr/gstpipe.vala.c:173:2: warning: ‘g_type_init’ is deprecated (declared at /usr/include/glib-2.0/gobject/gtype.h:669) [-Wdeprecated-declarations]


using Gst;


void application_message(Gst.Bus bus, Gst.Message msg) {

        var s = msg.get_structure();

        if(s == null)
            return;

        string msgtype = s.get_name();

        if(msgtype != "level")
            return;

        GLib.Value rms = s.get_value("rms");
        //GLib.Value st = s.get_value("stream-time");

        GLib.DateTime now = new GLib.DateTime.now_local();

        var sec = now.to_unix();
        var msec = (sec * 1000) + now.get_microsecond();        

        var z = rms.strdup_contents();

        //z = z.replace("{", "[").replace("}", "]");

        stdout.printf("%ld, %s \n", (long) msec, z);
}


void main (string[] args) {

    Gst.init (ref args);

    try {

        var pipeline = Gst.parse_launch(
          "pulsesrc device=\"alsa_input.usb-046d_08c9_674634A4-02-U0x46d0x8c9.analog-mono\" ! " +
          "level name=wavelevel interval=10000000 ! " +
          "wavenc ! filesink location=audioz.wav"
        );

        var bus = pipeline.get_bus();

        bus.add_signal_watch();
        bus.message.connect(application_message);

        // Set pipeline state to PLAYING
        pipeline.set_state (State.PLAYING);

        // Creating and starting a GLib main loop
        new MainLoop ().run ();        
    }
    catch(Error e) {
        print("%s\n", e.message);
    }
}
解决方案

You can generally ignore warnings from the C compiler when using Vala. Vala has better information than the C compiler, so it knows certain things are valid when the C compiler has no way of knowing that. Unfortunately we can't just add casts everywhere since there are situations where we can't generate a valid cast (and, what's more, no way to know what those situations are).

The final warning, about g_type_init being deprecated, is because g_type_init is no longer necessary as of glib 2.36. You can get rid of that warning by passing --target-glib=2.36 (or any later version of glib) to valac, but be warned that the generated code may no longer work with older versions of glib.

TBH, I often just pass -X -w to valac to get the C compiler to be quiet. Occasionally I miss a useful warning, but it gets rid of a lot of useless warnings.

这篇关于理解 vala 编译警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 12:51