Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 4, 2021

Conversation

cyrozap
Copy link
Contributor

@cyrozap cyrozap commented Mar 4, 2021

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
Copy link
Owner

r2axz commented Mar 4, 2021

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 r2axz added the bug Something isn't working label Mar 4, 2021
@r2axz
Copy link
Owner

r2axz commented Mar 4, 2021

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?

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.
@cyrozap
Copy link
Contributor Author

cyrozap commented Mar 4, 2021

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

@r2axz
Copy link
Owner

r2axz commented Mar 4, 2021

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.

@r2axz r2axz merged commit 2f12c77 into r2axz:main Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants