gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:22 24 4
gpt4 key购买 nike

我正在尝试从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com