编译Linux v5.4.0内核模块时出现eth_hw_addr_set隐式声明错误
内核模块编译错误:
eth_hw_addr_set 隐式声明问题 环境信息
- 系统:Ubuntu Server 20.04.6
- 已安装依赖:gcc、make、linux-headers-5.4.0-150-generic
- 内核版本:
uname -r输出5.4.0-150-generic,内核头文件版本匹配 - 编译目标:infinitv_pcie项目的内核模块
编译错误信息
/home/robert/ceton-drivers/infinitv_pcie-master/ctn91xx_net.c: In function ‘ctn91xx_net_init’: /home/robert/ceton-drivers/infinitv_pcie-master/ctn91xx_net.c:84:5: error: implicit declaration of function ‘eth_hw_addr_set’; did you mean ‘eth_addr_dec’? [-Werror=implicit-function-declaration] 84 | eth_hw_addr_set(netdev,addr); | ^~~~~~~~~~~~~~~ | eth_addr_dec
出错函数源代码
int ctn91xx_net_init(ctn91xx_dev_t* dev) { struct net_device *netdev = NULL; ctn91xx_net_priv_t *priv = NULL; u8 addr[6]; int i; netdev = alloc_etherdev(sizeof(ctn91xx_net_priv_t)); if (netdev == NULL) { ERROR("unable to alloc new ethernet"); return -ENOMEM; } #if USE_PCI SET_NETDEV_DEV(netdev, &dev->pdev->dev); #endif priv = netdev_priv(netdev); priv->ctn91xx_dev = dev; spin_lock_init(&priv->lock); snprintf(netdev->name, 20, "ctn%d", dev->board_number); #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29) netdev->netdev_ops = &ctn91xx_net_device_ops; #else netdev->open = ctn91xx_net_open; netdev->hard_start_xmit = ctn91xx_net_start_xmit; netdev->stop = ctn91xx_net_stop; netdev->get_stats = ctn91xx_net_get_stats; netdev->tx_timeout = ctn91xx_net_tx_timeout; #endif netdev->watchdog_timeo = CTN91XX_NET_TX_TIMEOUT; i = register_netdev(netdev); if (i) { ERROR("failed to register netdev"); free_netdev(netdev); return -EINVAL; } #if USE_PCI addr[0] = 0x00; addr[1] = 0x22; addr[2] = 0x2c; addr[3] = 0xff; addr[4] = 0xff; addr[5] = 0xff - dev->board_number; eth_hw_addr_set(netdev,addr); #else random_ether_addr(netdev->dev_addr); #endif netdev->irq = dev->irq; netdev->base_addr = (unsigned long)dev->hw_reg_base; dev->net_dev = netdev; #if USE_PCI ctn91xx_write8(1, dev->msg_base, MSG_BUFFER_INT_ENABLE); #endif return 0; }
解决方法
方法1:添加必要的头文件
eth_hw_addr_set函数定义在<linux/etherdevice.h>中,检查ctn91xx_net.c文件顶部是否包含该头文件,如果没有,添加:
#include <linux/etherdevice.h>
方法2:替换为内核兼容的写法
如果添加头文件后仍报错,说明Ubuntu 20.04的5.4内核版本不支持eth_hw_addr_set函数,可以直接将MAC地址复制到网卡设备的dev_addr字段,替换原有代码:
把
eth_hw_addr_set(netdev,addr);
替换为:
memcpy(netdev->dev_addr, addr, ETH_ALEN);
如果代码中没有包含<linux/string.h>(memcpy的头文件),需要在文件顶部添加:
#include <linux/string.h>
内容的提问来源于stack exchange,提问作者Robert Rathbone
相关产品推荐
相关产品推荐

