代码之家  ›  专栏  ›  技术社区  ›  md.jamal

linux设备驱动程序中存在多个定义错误[重复]

  •  0
  • md.jamal  · 技术社区  · 6 年前

    这个阵列由两个USB Linux驱动程序用于我的定制板。

    #ifndef PIC_FIRMWARE_H
    #define PIC_FIRMWARE_H
    
    const unsigned char ucPICAppsectorFirmware[] = 
    {
      0xa5,0xef,0x2b,0xf0, 0x12,0x00,0x12,0x00, // Address 0x3000
      0x81,0xef,0x29,0xf0, 0x12,0x00,0x12,0x00, // Address 0x3008
      0x00,0x00,0xff,0xff, 0xff,0xff,0xff,0xff, // Address 0x3010
      0xab,0xef,0x29,0xf0, 0x12,0x00,0xff,0xff, // Address 0x3018
    ....
    }
    #endif
    

    当我将这两个驱动程序都添加为内置驱动程序并在这两个驱动程序代码中包含头文件(#include“picŠu firmware.h”)时,我得到了多个定义错误

    | drivers/usb/misc/pic_dfu.o:(.rodata+0x80): multiple definition of `ucPICAppsectorFirmware'
    | drivers/usb/misc/usb_mib.o:(.rodata+0xcc0): first defined here
    

    如何解决此错误。谢谢你的时间。。

    2 回复  |  直到 6 年前
        1
  •  4
  •   nos    6 年前

    有3种常见的选择。


    1. 使用该.c文件中的固件所需的函数。在头文件中公开这些函数。

    2. 将数组设为静态,使其在其他转换单元中不可见:

    static const unsigned char ucPICAppsectorFirmware[] = ....
    

    请注意,这将在包含此头文件的每个.c文件中创建数组的副本。

    i、 头文件看起来像。

    extern const unsigned char ucPICAppsectorFirmware[];
    extern const size_t ucPICAppsectorFirmwareLen;
    

    c文件看起来像

    const unsigned char ucPICAppsectorFirmware[] = ...;
    const size_t ucPICAppsectorFirmwareLen = sizeof ucPICAppsectorFirmware;
    
        2
  •  1
  •   P.W    6 年前

    因为 ucPICAppsectorFirmware UCPICAppSector固件 pic_firmware.h .

    这就是导致多定义链接器错误的原因。