Arduino build error

undefined reference to `readSensor()'

The code compiled. The linker, which joins the pieces, could not find the body of a function that your code calls.

  • Build error
  • 2 common causes
  • Seen 165 times in failed builds on Mokxi in the 30 days to November 16, 2026.
The message

What the Arduino IDE prints

Output window
sketch.ino.cpp.o: In function `setup':
sketch.ino.cpp.o: undefined reference to `readSensor()'
collect2: error: ld returned 1 exit status
exit status 1

The function was announced but never written, or it was written with different parameters, or its file is not part of the build.

Mokxi's linker, lld, words it as undefined symbol: readSensor().

Causes

The 2 usual causes with the fix

1A function is declared and has no body

A line such as int readSensor(); only announces the function. Write its body, or remove the call.

Broken
int readSensor();

void setup() {
  Serial.begin(9600);
  Serial.println(readSensor());
}

void loop() {
}
Fixed
int readSensor() {
  return 42;
}

void setup() {
  Serial.begin(9600);
  Serial.println(readSensor());
}

void loop() {
}

Mokxi’s build tab says: readSensor is used, but its code is in no file of the build.

Try this fix in Mokxi

2The body has different parameters than the declaration

A function is known by its name and its parameter types together. int blink(int) and int blink() are two functions.

Broken
int blink(int times);

int blink() {
  return 1;
}

void setup() {
  Serial.begin(9600);
  Serial.println(blink(3));
}

void loop() {
}
Fixed
int blink(int times) {
  return times;
}

void setup() {
  Serial.begin(9600);
  Serial.println(blink(3));
}

void loop() {
}

Mokxi’s build tab says: blink is used, but its code is in no file of the build.

Try this fix in Mokxi

FAQ

Questions and answers

What does "undefined reference" mean?

The linker found a call to a function and no definition of it anywhere in the build.

Why does it appear after the code compiled?

Compiling checks each file alone. Linking joins them and only then finds what is missing.

Can a library cause it?

Yes, when a header was included and the matching .cpp file is not part of the build. Reinstall the library, and in a project add its .cpp file.