0

The linker is failing by thowing following error

ltc E108: multiple definitions of symbol "file__img_sics_gif" in both "fsdata.o" and "fs.o"
ltc E108: multiple definitions of symbol "file__404_html" in both "fsdata.o" and "fs.o"
ltc E108: multiple definitions of symbol "file__index_html" in both "fsdata.o" and "fs.o"
make: *** [makefile:95: Ethernet_1_KIT_TC397_TFT.elf] Error 1

The structs file__img_sics_gif, file__404_html, file__index_html are defined in fsdata.c:

const struct fsdata_file file__img_sics_gif[] = {{
file_NULL,
data__img_sics_gif,
data__img_sics_gif + 16,
sizeof(data__img_sics_gif) - 16,
FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT,
}};

const struct fsdata_file file__404_html[] = {{
file__img_sics_gif,
data__404_html,
data__404_html + 12,
sizeof(data__404_html) - 12,
FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT,
}};

const struct fsdata_file file__index_html[] = {{
file__404_html,
data__index_html,
data__index_html + 12,
sizeof(data__index_html) - 12,
FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT,
}};

#define FS_ROOT file__index_html
#define FS_NUMFILES 3

which I include in fs.c and access through the macro FS_ROOT. The struct fsdata_file is defined in fs.h.

  struct fsdata_file {
  const struct fsdata_file *next;
  const unsigned char *name;
  const unsigned char *data;
  int len;
  u8_t flags;
  #if HTTPD_PRECALCULATED_CHECKSUM
  u16_t chksum_count;
  const struct fsdata_chksum *chksum;
  #endif /* HTTPD_PRECALCULATED_CHECKSUM */
  }; 
1
  • 2
    You should never define variables in files you include in other files. Only put declarations there. That said, you should not include C files in other C files. Instead compile them separately and link together. According to the linker error you include it and compile it standalone. That results in 2 definitions of same variables.
    – Gerhardh
    Jun 20, 2023 at 13:16

1 Answer 1

3

The structs file__img_sics_gif, file__404_html, file__index_html are defined in fsdata.c

That's fine.

which I include in fs.c and access through the macro FS_ROOT.

That's not ok.

Although the preprocessor will let you do it, you should never #include one .c file into another. Supposing that you also make sure never to put object or function definitions in your headers (only their declarations), that will help ensure that you do not have exactly the kind of multiple-definition errors you describe. If you do include one .c in another, then you should not compile or link the included file independently, nor include it in more than one other file.

As a general rule, you should not use "global" variables, either, but if you insist on doing so then you put their definitions in a single source file each, and put extern declarations of them in a header. Other sources that want to access them include only the header, and the linker hooks everything up. Example:

fs.h

struct fsdata_file {
  const struct fsdata_file *next;
  const unsigned char *name;
  const unsigned char *data;
  int len;
  u8_t flags;
  #if HTTPD_PRECALCULATED_CHECKSUM
  u16_t chksum_count;
  const struct fsdata_chksum *chksum;
  #endif /* HTTPD_PRECALCULATED_CHECKSUM */
}; 

extern const struct fsdata_file file__img_sics_gif[];
extern const struct fsdata_file file__404_html[];
extern const struct fsdata_file file__index_html[];

Note well, however, that those particular declarations do not convey the array lengths. That's ok as far as the linker is concerned, and it may be ok for your code that uses the arrays, but if you need the lengths outside the source file that defines these arrays then you'll need to hardcode them or to convey them via separate variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.