Undefined reference to 'tone' after including Arduino.h

Hi there! First time poster here. I'm trying to learn how to code directly in C or C++ in Arduino and I've been reading a lot from Nick's great posts such as this one: http://www.gammon.com.au/forum/?id=12625

So I'm trying to translate an .ino sketch file into a more traditional C sketch.

EDIT: I'm a ding-dong and Tone is a cpp file, so it makes sense the compiler would be confused. Thanks again PieterP!

Here's the original sketch that compiles correctly.

// Arduino pin numbers
const int SW_pin = 8; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
const int AUDIO_OUT = 7; //audio output
 
void setup() {
  pinMode(SW_pin, INPUT);
  pinMode(AUDIO_OUT, OUTPUT);
  digitalWrite(SW_pin, HIGH);
}

int joyX, joyY, freq;
const double MAX_FREQ = 500;
void loop() {
  joyX = analogRead(X_pin);
  joyY = analogRead(Y_pin);
  freq = 60 + ((MAX_FREQ * joyX) / 1024 - 1);
  tone(AUDIO_OUT, freq);
}

I've attached a picture of my setup and didn't bother with including a schematic since this code works correctly and I'm using the same hardware setup for both sketches. I can flash the above to my Arduino Uno and it sounds and behaves as expected. This particular hardware setup involves mapping a joystick's X values to a tone's pitch that's being played out of a buzzer.

Everything works great there, so now I'm trying to just port this to straight C using the Arduino.h file. As it's been suggested, I've left my main sketch file empty and named it "C_ToneToArduino" and opened up a new tab named "toneToArduino.c".

The main sketch is completely empty - here's the toneToArduino.c file:

#include <Arduino.h>

const int SWITCH_IN = 8; // digital pin connected to switch output
const int X_PIN = 0; // analog pin connected to X output
const int Y_PIN = 1; // analog pin connected to Y output
const int AUDIO_OUT = 7; //audio output
const double MAX_FREQ = 500.0;

int joyX, joyY, freq;

void main() {
  pinMode(SWITCH_IN, INPUT);
  pinMode(AUDIO_OUT, OUTPUT);
  digitalWrite(SWITCH_IN, HIGH);

  while (true) {
    joyX = analogRead(X_PIN);
    joyY = analogRead(Y_PIN);
    freq = 60 + ((MAX_FREQ * joyX) / 1024 - 1);
    tone(AUDIO_OUT, freq); 
  }
}

When I try to verify this, I get the following compilation error:

cc49aDPI.ltrans0.ltrans.o: In function `main':
sketch/toneToArduino.c:20: undefined reference to `tone'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Uno.

Looking at the Arduino.h file on Github - I can clearly see the function prototype on line 247:

void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);

The tone() method doesn't seem to be in any namespace either. Looking around on this forum, I saw that other people encountering this error kept naming their sketch files "Tone" which caused them compilation errors but that's not my problem here.

Any help would be greatly appreciated! Thanks :slight_smile:

Tone is a C++ function (source), you cannot call it from C code. What are your reasons for using C? 99% of Arduino libraries are written in C++, and all Arduino sketches are compiled as C++ as well.

Pieter

PieterP:
Tone is a C++ function (source), you cannot call it from C code. What are your reasons for using C? 99% of Arduino libraries are written in C++, and all Arduino sketches are compiled as C++ as well.

Pieter

Ah thank you, I feel silly now :stuck_out_tongue:

Purely academic, I've been teaching myself audio signal processing in C and also teaching myself C++ because I'm used to object oriented design in C# - just thought that C is a leaner language than C++ so I figured that'd be the way to go for microcontrollers. Thanks again, that makes sense I don't know why I didn't bother checking out the rest of the GitHub repo

astinad:
just thought that C is a leaner language than C++ so I figured that'd be the way to go for microcontrollers.

The same code in C++ is just as efficient as in C. You don't pay for what you don't use in C++.

Personally, I see no reason to prefer C over C++, even on microcontrollers. C++ does everything C does and much more, and it does it better and safer.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.