r2axz / bluepill-serial-monster

USB to 3 Port Serial (UART) adapter firmware for STM32 Blue Pill.
MIT License
323 stars 76 forks source link

Fix build failure due to "multiple definition" errors #27

Closed cyrozap closed 3 years ago

cyrozap commented 3 years ago

The USB descriptor variables were effectively being declared twice--once in usb_descriptors.c and once in usb_core.c--because both files included usb_descriptors.h, where those variables were originally declared without the "extern" keyword. Because of this, GCC refused to link the object files. After declaring the variables as "extern", the build finishes without issue.

Here's an example of the issue this PR fixes:

/usr/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/bin/ld: build/usb_descriptors.o:/src/bluepill-serial-monster/usb_descriptors.h:96: multiple definition of `usb_configuration_descriptor'; build/usb_core.o:/src/bluepill-serial-monster/usb_descriptors.h:96: first defined here
/usr/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/bin/ld: build/usb_descriptors.o:/src/bluepill-serial-monster/usb_descriptors.h:58: multiple definition of `usb_device_descriptor'; build/usb_core.o:/src/bluepill-serial-monster/usb_descriptors.h:58: first defined here
/usr/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/bin/ld: build/usb_descriptors.o:/src/bluepill-serial-monster/usb_descriptors.h:31: multiple definition of `usb_string_descriptors'; build/usb_core.o:/src/bluepill-serial-monster/usb_descriptors.h:31: first defined here
/usr/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/bin/ld: build/usb_io.o:/src/bluepill-serial-monster/usb_descriptors.h:96: multiple definition of `usb_configuration_descriptor'; build/usb_core.o:/src/bluepill-serial-monster/usb_descriptors.h:96: first defined here
/usr/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/bin/ld: build/usb_io.o:/src/bluepill-serial-monster/usb_descriptors.h:58: multiple definition of `usb_device_descriptor'; build/usb_core.o:/src/bluepill-serial-monster/usb_descriptors.h:58: first defined here
/usr/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/bin/ld: build/usb_io.o:/src/bluepill-serial-monster/usb_descriptors.h:31: multiple definition of `usb_string_descriptors'; build/usb_core.o:/src/bluepill-serial-monster/usb_descriptors.h:31: first defined here
/usr/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/bin/ld: build/usb_cdc.o:/src/bluepill-serial-monster/usb_descriptors.h:96: multiple definition of `usb_configuration_descriptor'; build/usb_core.o:/src/bluepill-serial-monster/usb_descriptors.h:96: first defined here
/usr/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/bin/ld: build/usb_cdc.o:/src/bluepill-serial-monster/usb_descriptors.h:58: multiple definition of `usb_device_descriptor'; build/usb_core.o:/src/bluepill-serial-monster/usb_descriptors.h:58: first defined here
/usr/lib/gcc/arm-none-eabi/10.2.0/../../../../arm-none-eabi/bin/ld: build/usb_cdc.o:/src/bluepill-serial-monster/usb_descriptors.h:31: multiple definition of `usb_string_descriptors'; build/usb_core.o:/src/bluepill-serial-monster/usb_descriptors.h:31: first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:52: bluepill-serial-monster.elf] Error 1
r2axz commented 3 years ago

This certainly looks like a bug, thank you. However, I still need to find out why it builds on my machine as well with GitHub CI.

r2axz commented 3 years ago

OK, I confirm the issue, but I don't believe you fixed it in the right way. The only thing that is really needed is to add missing "extern" modifiers into usb_descriptors.h. There is no need to remove those declarations and move them to usb_core.c. There is no need to touch usb_core.c at all.

Here is what I suggest:

  1. Roll back changes to usb_core.c;
  2. Roll back changes to usb_descriptors.c;
  3. Add extern modifiers to usb_descriptors.h like this:
extern const usb_string_descriptor_t *usb_string_descriptors[usb_string_index_last];

Actually, I believe the simplest way is to roll back everything and then add missing extern modifiers to usb_descriptors.h

Thoughts?

cyrozap commented 3 years ago

I can confirm that your suggested fix also fixes the issue for me. I've updated this PR accordingly.

r2axz commented 3 years ago

Looks great! Thank you for identifying the issue and for your contribution to the project. It this point I am ready to accept the PR.