MCUdude / MegaCoreX

An Arduino hardware package for ATmega4809, ATmega4808, ATmega3209, ATmega3208, ATmega1609, ATmega1608, ATmega809 and ATmega808
GNU Lesser General Public License v2.1
247 stars 52 forks source link

ATmega4808: Arduino auto-detect libraries finds wrong Servo library -> Error: multiple definition of __vector_25 #167

Closed ArminJo closed 1 year ago

ArminJo commented 1 year ago

I try to compile Servo code for a ATmega4808, as can be found here: This is the error i get:

(.text+0x0): multiple definition of `__vector_25'
...
 Multiple libraries were found for "Servo.h"
  Used: /home/runner/Arduino/libraries/Servo
  Not used: /home/runner/.arduino15/packages/MegaCoreX/hardware/megaavr/1.1.0/libraries/Servo
Using library ServoEasing at version 3.1.0 in folder: /home/runner/work/ServoEasing/ServoEasing 

I get the same error on my windows Arduino 1.8 and 2.0 IDEs. The compile error vanishes if I enable USE_TIMERB1instead of USE_TIMERB2in the Arduino Servo megaavr/ServoTimers.h file.

Do you know any workaround apart from deleting the Arduino Servo library manually? And what is the use case of having a Servo library not used by Arduino? Or is this an Arduino library detection bug (which will never be fixed, I assume)?

Thanks for caring Armin

per1234 commented 1 year ago

is this an Arduino library detection bug

No. This is the expected result from the system working as designed:

https://arduino.github.io/arduino-cli/latest/sketch-build-process/#dependency-resolution

The two libraries have the same score for "architecture matching", "library name", and "folder name" priorities, so the tie-breaker is the "location priority" score:

The "location priority" is determined as follows (in order of highest to lowest priority): [...]

  1. The library is under the libraries subfolder of the IDE's sketchbook or Arduino CLI's user directory
  2. The library is bundled with the board platform/core ({runtime.platform.path}/libraries)

Something to note is most users will have an additional copy of the global Servo library installed in a separate location. This location has lower priority than the platform bundled library:

  1. The library is bundled with the Arduino IDE (this location is determined by the Arduino CLI configuration setting directories.builtin.libraries)

This means that after uninstalling the Servo library from the sketchbook folder, a global installation of Servo library would still be available when compiling for other boards without a platform bundled Servo library, while the platform bundled library gets priority when compiling for a MegaCoreX board.

Do you know any workaround apart from deleting the Arduino Servo library manually?

For your GitHub Actions workflow, simple remove Servo from the env.REQUIRED_LIBRARIES key and add it to the matrix.required-libraries key (example) of the boards that don't have a platform bundled version of the Servo library.

As for normal human usage of MegaCoreX, the workaround would be to add a header file with a unique name (e.g., ServoMegaCoreX.h to the platform bundled library). If the program contains an #include directive for this header before the #include directive for Servo.h, this will cause the intended platform bundled library to be discovered instead of the one in the sketchbook. The header could be empty, or even act as a substitute for including Servo.h:

ServoMegaCoreX.h

#include <Servo.h>
ArminJo commented 1 year ago

https://arduino.github.io/arduino-cli/latest/sketch-build-process/#dependency-resolution

Thanks for the link, I searched for it for half an hour, before starting this issue.

For your GitHub Actions workflow, simple remove Servo from the env.REQUIRED_LIBRARIES key and add it to the matrix.required-libraries key (example) of the boards that don't have a platform bundled version of the Servo library.

This indeed works for me 😀 👍 T H A N K S ! ! !

@MCUdude How can I decide, that the MEGAX kernel is used? I am looking for something like this

if defined MEGAXCORE

@ All Happy new year

MCUdude commented 1 year ago

How can I decide, that the MEGAX kernel is used?

It's all there in the README: https://github.com/MCUdude/MegaCoreX#identifying-megacorex

ArminJo commented 1 year ago

Thanks for adding ServoMegaCoreX.h 👍