-1

I want to try out this library: https://github.com/connornishijima/arduino-volume3

I downloaded the zip file from github, renamed it, and included it as a library in the IDE.

So run one of the examples, A4_440HZ This is its code:

#include "Volume3.h"
#define speakerPin 9

uint16_t frequency = 440;

void setup() {
  // Nothing neeeded here!
}

void loop() {  
  for(uint16_t volume = 0; volume < 1023; volume++){
    vol.tone(speakerPin,frequency,volume);
    delay(1);
  }
  for(uint16_t volume = 1023; volume > 0; volume--){
    vol.tone(speakerPin,frequency,volume);
    delay(1);
  }
}

And i get this error:

Error Compiling
A4_440Hz.cpp.o: In function `loop':
/usr/share/arduino/A4_440Hz.ino:12: undefined reference to `vol'
/usr/share/arduino/A4_440Hz.ino:12: undefined reference to `vol'
/usr/share/arduino/A4_440Hz.ino:16: undefined reference to `vol'
/usr/share/arduino/A4_440Hz.ino:16: undefined reference to `vol'
collect2: error: ld returned 1 exit status

I have no idea what is to blame, or how to mitigate it.

This is the first time that an included example from a library does not play out of the box.

6
  • It compiles here. What IDE version are you running?
    – timemage
    Commented Jun 24, 2022 at 16:26
  • You could have just installed the library from the library manager in the Arduino IDE.
    – StarCat
    Commented Jun 24, 2022 at 16:39
  • They are using extern Volume vol; in header file, but there is no definition in cpp, nor in the sketch. The Volume vol; will be needed in your code, I suppose
    – KIIV
    Commented Jun 24, 2022 at 17:27
  • The second part of that library, the TimerOne uses the same pattern, but it has both the declaration in header file and the definition in cpp file line 41 TimerOne.cpp
    – KIIV
    Commented Jun 24, 2022 at 17:31
  • @timemage It is 2:1.0.5. It does not have a library manager. Perhaps it is an old version? But i installed it recently. I am using Ubuntu Linux. Commented Jun 24, 2022 at 18:46

1 Answer 1

2

This is kind of a weird situation. The library declares the object vol, but fails to define it. On the ancient version of the Arduino IDE you are using, compilation fails, understandably, because vol has never been defined. What is weird is that, on a more recent version (1.8.15 on Ubuntu here), compilation succeeds.

It is worth noting that vol belongs to the class Volume, which is an empty class. As such, it only plays the role of a namespace for the functions vol.tone() and vol.noTone(). Strictly speaking, as vol has no data (its size is zero), the compiler should never need to access it. My guess is that the newer Arduino manages to compile this only because it uses different optimization settings than the ancient 1.0.5.

Anyhow, you have two options:

  • upgrade your installation of the Arduino IDE

  • declare Volume vol; near the top of the sketch.

Edit: I did some tests and it appears that, as I suspected, this is related to link-time optimization (LTO). Since version 1.5.7, the Arduino IDE enables LTO, and when this is the case, the linker doesn't complain about missing empty objects.

The library could be fixed by defining vol in Volume3.cpp, although this would create a weird empty object in the BSS. Another option would be to declare both methods as static within Volume3.h. This would make the compiler know it doesn't actually need to reference the vol object when one of its methods is called.

Would you submit a pull request with one of those fixes?

5
  • Thank you! I tried both! The library compiles but no sound is heard. I guess it is an old library. Commented Jun 24, 2022 at 20:22
  • Same regarding optimizer. To be clear, the code was always incorrect. It is only a question of whether the problem manifests from that.
    – timemage
    Commented Jun 24, 2022 at 20:23
  • So if i do your suggestions, will the lib work? Did you manage to make it run? BTW, the last sentence is this targeted to me? Commented Jun 24, 2022 at 22:20
  • @user1584421: It should work, although I tested only on example code with my host compiler. And yes, the last sentence was a suggestion for you. Commented Jun 24, 2022 at 22:38
  • Thank you. But inside Volume3.cpp i defined Volume vol;. I then used one of the examples.The library compiled/uploaded but nothing is heard from the speakers. Commented Jun 29, 2022 at 23:30

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.