c++ - netlink 接口(interface) stat goto 交叉初始化错误

标签 c++ c linux netlink

我正在尝试从 netlink 库中读取接口(interface)统计信息

下面是代码:

#include <iostream>
#include <stdio.h>
#include <netlink/netlink.h>
#include <netlink/route/link.h>
#include <unistd.h>

using namespace std;

int main(int argc, char *argv[])
{
    //cout << "Hello world!" << endl;

    //if (argc != 2){
    //    printf("usage %s interface\n ", argv[0]);
    //}

    int ret = 0;
    struct nl_handle *handle;

    //Allocate and initialize a netlink handle
    handle = nl_handle_alloc();
    if (handle == 0){
        printf("Failed to allocate a netlink handle\n");
        ret = -1;
        goto error_handle;
    }

    //bind and connect the socket to a protocol
    ret = nl_connect(handle, NETLINK_ROUTE);
    if (ret != 0){
        printf("failed to connect to kernel\n");
        ret = -1;
        goto error_connect;
    }

    //join a group to receive notifications
    ret = nl_socket_add_membership(handle, RTNLGRP_LINK);
    if (ret != 0){
        printf("joining group failed\n");
        ret = -1;
        goto error_connect;
    }

    //retrieve list of all interfaces and put them in cache
    struct nl_cache *cache = rtnl_link_alloc_cache(handle);
    if ( cache == 0 ){
        printf("error creating link cache\n");
        ret = -1;
        goto error_connect;
    }

    //lookup interface
    struct rtnl_link *link = rtnl_link_get_by_name(cache, "ens33");
    if (link == 0){
        printf("No such interface %s\n", "ens33");
        ret = -1;
        goto error_cache;
    }

    printf("packets sent %llu\n", rtnl_link_get_stat(link, RTNL_LINK_TX_PACKETS));
    printf("packets received %llu\n",rtnl_link_get_stat(link, RTNL_LINK_RX_PACKETS));



    //give the object back to the cache
    rtnl_link_put(link);
    error_cache:
        nl_cache_free(cache);

    error_connect:
        nl_handle_destroy(handle);

    error_handle:
        return ret;

    return 0;
}

但是,我得到这个错误......

-------------- Build: Debug in KSHK-Netlink01 (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -g -I/usr/include/netlink -c /home/kshk/ic++/KSHK-Netlink01/main.cpp -o obj/Debug/main.o
/home/kshk/ic++/KSHK-Netlink01/main.cpp: In function ‘int main(int, char**)’:
/home/kshk/ic++/KSHK-Netlink01/main.cpp:73:5: error: jump to label ‘error_connect’ [-fpermissive]
     error_connect:
     ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:44:14: error:   from here [-fpermissive]
         goto error_connect;
              ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:56:23: error:   crosses initialization of ‘rtnl_link* link’
     struct rtnl_link *link = rtnl_link_get_by_name(cache, "ens33");
                       ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:48:22: error:   crosses initialization of ‘nl_cache* cache’
     struct nl_cache *cache = rtnl_link_alloc_cache(handle);
                      ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:73:5: error: jump to label ‘error_connect’ [-fpermissive]
     error_connect:
     ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:36:14: error:   from here [-fpermissive]
         goto error_connect;
              ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:56:23: error:   crosses initialization of ‘rtnl_link* link’
     struct rtnl_link *link = rtnl_link_get_by_name(cache, "ens33");
                       ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:48:22: error:   crosses initialization of ‘nl_cache* cache’
     struct nl_cache *cache = rtnl_link_alloc_cache(handle);
                      ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:76:5: error: jump to label ‘error_handle’ [-fpermissive]
     error_handle:
     ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:28:14: error:   from here [-fpermissive]
         goto error_handle;
              ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:56:23: error:   crosses initialization of ‘rtnl_link* link’
     struct rtnl_link *link = rtnl_link_get_by_name(cache, "ens33");
                       ^
/home/kshk/ic++/KSHK-Netlink01/main.cpp:48:22: error:   crosses initialization of ‘nl_cache* cache’
     struct nl_cache *cache = rtnl_link_alloc_cache(handle);
                      ^
Process terminated with status 1 (0 minute(s), 0 second(s))
12 error(s), 0 warning(s) (0 minute(s), 0 second(s))

有什么建议吗?

最佳答案

你不能跳过变量声明。如果要使用 goto,则必须在第一个 goto 之前声明所有变量。

关于c++ - netlink 接口(interface) stat goto 交叉初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24492511/

相关文章:

linux - 如何将程序和文件的组合输出显示为表格并在 1 秒内观看

linux - nl 和 tail 的行号之间的差异

c++ - setData 为 QAbstractProxyModel 返回 false

c++ - C 和 C++ 之间的区别 ( lseek() )

c++ - C++ 中 openMP Shared 子句的用法

c - 正常 block 后检测到堆损坏(#55)

c - 处理 C 中的特殊字符(UTF-8 编码)

c - fork() 和 pipeline() 的竞争条件

linux - webkit-gtk 编译错误

c++ - Googletest 参数化测试崩溃