1

I have my firmware (array) for PIC stored in a header file (pic_firmware.h)..

This array is used by two USB Linux drivers for my customized board.

#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

When I add the both drivers as builtin and include the header file (#include "pic_firmware.h") in both driver code, I am getting multiple definition error

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

How can I resolve this error. Thanks for your time..

md.jamal
  • 4,067
  • 8
  • 45
  • 108

2 Answers2

5

There's 3 common options.

  1. Don't define the firmware in a header file, define it in a .c file and create the
    functions you need to work with that firmware within that .c file. Expose those functions in your header file.

  2. Make the array static, so it's not visible in other translation units:

like so:

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

Note that this will create a copy of the array in every .c file where you include this header file.

  1. Place the array in a .c file, and declare it instead of defining it in the header file. This way the array would not be duplicated for each file that includes it, as would be the case when defining the array in the header file.

i.e. the header file would look like.

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

and the .c file would look like

const unsigned char ucPICAppsectorFirmware[] = ...;
const size_t ucPICAppsectorFirmwareLen = sizeof ucPICAppsectorFirmware;
nos
  • 223,662
  • 58
  • 417
  • 506
1

Because ucPICAppsectorFirmware is a definition and not just a declaration, you must declare ucPICAppsectorFirmware as extern in the header file, otherwise the definition will exist in each of the files which includes the pic_firmware.h.

This is what causes the multiple definition linker error.

P.W
  • 26,289
  • 6
  • 39
  • 76