Home Learning Common Arduino Compilation Errors and Fixes (With Examples)

Common Arduino Compilation Errors and Fixes (With Examples)

by shedboy71
[lebox id="1"]

Arduino compilation errors usually fall into a few predictable buckets:

  • Syntax errors: missing semicolons, braces, quotes
  • Name/Scope errors: variables/functions not declared, wrong casing
  • Type errors: mixing incompatible types, wrong function signatures
  • Library errors: missing libraries, wrong versions, wrong include names
  • Board/port setup errors: wrong board selected, missing core packages
  • C/C++ gotchas: prototypes, header guards, multiple definitions

This guide talks about the most common compile-time messages, what they mean, and how to fix them. There are a lot of short code examples in it that you can copy and paste into the IDE to see the problem and figure it out.

Quick Checklist

Before digging into the specific error message:

  1. Read the first error in the output panel (later errors are often “noise”).
  2. Confirm Tools → Board matches your actual board.
  3. Confirm Tools → Processor (if applicable) matches (common with Nano).
  4. If the error mentions a header like SomeLib.h, verify the library is installed.
  5. If it mentions avr-g++ / xtensa-esp32-elf-g++ / similar, it’s a compile step issue (not upload).
  6. If it mentions ld or “linking”, it’s usually duplicate definitions or missing implementations.

1) expected ';' before ...

What it means

You missed a semicolon at the end of a statement, or you ended a line in a way the compiler can’t interpret.

Example that fails

void setup() {
  int x = 10
  Serial.begin(9600);
}
void loop() {}

Fix

void setup() {
  int x = 10;
  Serial.begin(9600);
}
void loop() {}

Common hidden cause: missing semicolon after a struct

struct Config {
  int threshold;
}  // missing semicolon here

void setup() {}
void loop() {}

Fix:

struct Config {
  int threshold;
};

2) expected '}' at end of input

What it means

You opened a { block but didn’t close it.

Example that fails

void setup() {
  if (true) {
    // do something
}

void loop() {}

Fix

void setup() {
  if (true) {
    // do something
  }
}

void loop() {}

Tip

In Arduino IDE, click near a brace and it highlights the matching pair. If it doesn’t, you’ve likely got an imbalance.

3) 'xyz' was not declared in this scope

What it means

You used a variable or function name the compiler can’t see where you used it. Usually:

  • Misspelling / wrong capitalization
  • Declared inside a different block (scope issue)
  • Missing function prototype (more common in .cpp files than .ino)

Example that fails: scope problem

void setup() {
  int ledPin = 13;
}

void loop() {
  digitalWrite(ledPin, HIGH); // ledPin not visible here
}

Fix: make it global

int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
}

Example that fails: capitalization

int LedPin = 13;

void setup() {
  pinMode(ledpin, OUTPUT); // wrong casing
}
void loop() {}

Fix:

pinMode(LedPin, OUTPUT);

4) redefinition of '...'

What it means

You defined the same variable/function twice in the same translation unit.

Example that fails

int value = 10;
int value = 20; // redefinition

void setup() {}
void loop() {}

Fix

Rename one variable or remove the duplicate:

int value = 10;
int value2 = 20;

5) multiple definition of '...' (Linker error)

What it means

You defined the same global symbol in more than one file (common when using .h files incorrectly).

Classic cause: defining globals in a header

Bad: MyVars.h

int counter = 0; // definition in header (bad)

Included in multiple .cpp/.ino files → multiple definitions.

Fix approach

MyVars.h

extern int counter; // declaration only

MyVars.cpp

int counter = 0; // single definition

6) undefined reference to '...' (Linker error)

What it means

You declared a function but never provided its implementation, or you forgot to compile/link the file that contains it.

Example that fails

void doThing(); // declared but not defined

void setup() {
  doThing();
}
void loop() {}

Fix

void doThing() {
  // implementation
}

void setup() {
  doThing();
}
void loop() {}

Another common cause

You created a .cpp file but didn’t place it inside the sketch folder (or library folder) correctly, so it never builds.

7) no matching function for call to ...

What it means

You called a function with the wrong number or types of arguments.

Example that fails

void setup() {
  pinMode(13); // missing second argument
}
void loop() {}

Fix

void setup() {
  pinMode(13, OUTPUT);
}
void loop() {}

Another example: wrong type

void setup() {
  digitalWrite(13, 2); // should be HIGH/LOW
}
void loop() {}

Fix:

digitalWrite(13, HIGH);

8) invalid conversion from '...' to '...'

What it means

You’re forcing a type conversion the compiler won’t allow safely.

Example that fails: string literal to char*

void setup() {
  char* name = "Arduino"; // string literal is const char[]
}
void loop() {}

Fix

void setup() {
  const char* name = "Arduino";
}
void loop() {}

Example that fails: float to int pointer (typical library misuse)

int* p = 3.14; // nonsense conversion

Fix: use correct type or correct variable:

float x = 3.14;

9) conflicting declaration / conflicting types for ...

What it means

You declared the same function/variable with different types.

Example that fails

int readSensor();
float readSensor() { return 1.2; } // conflict

Fix

Make the declaration match the definition:

float readSensor() { return 1.2; }

10) expected primary-expression before ...

What it means

Often caused by:

  • Missing parentheses
  • Extra commas
  • Using = instead of ==
  • Broken function call syntax

Example that fails

void setup() {
  int x = (5 + ); // incomplete expression
}
void loop() {}

Fix:

int x = (5 + 3);

Another common one: stray comma

digitalWrite(13, HIGH,);

Fix:

digitalWrite(13, HIGH);

11) stray '\xxx' in program

What it means

You pasted a weird character into the code: smart quotes, non-breaking spaces, or other invisible Unicode.

Example that fails

Serial.println(“Hello”); // smart quotes

Fix

Use normal quotes:

Serial.println("Hello");

Tip: retype quotes manually if you copied from a webpage.

12) #include <SomeLib.h>: No such file or directory

What it means

The compiler can’t find the library header. Causes:

  • Library not installed
  • Wrong header name
  • Library incompatible with selected board/core

Example that fails

#include <NonexistentLib.h>
void setup() {}
void loop() {}

Fix checklist

  • Install the library via Library Manager
  • Verify the header file name (case sensitive on some systems)
  • Make sure you selected the correct board core (AVR vs ESP32 vs SAMD)

13) Arduino.h: No such file or directory or core package errors

What it means

Board core is missing/corrupted or the wrong toolchain is being used.

Fix checklist

  • Re-select the board in Tools → Board
  • Reinstall the board core using Boards Manager
  • Restart the IDE after changes
  • If using Arduino CLI, ensure the core is installed

14) “Sketch too big” / text section exceeds available space

What it means

Your compiled code (Flash usage) exceeds the board’s program memory.

Example cause

  • Huge arrays not stored in Flash properly
  • Many libraries included
  • Debug prints and large strings

Fix strategies

  • Move large constants into Flash (AVR boards): PROGMEM
  • Remove unused libraries
  • Reduce Serial.print strings, or store them more efficiently
  • Choose a board with more Flash (Mega, ESP32, etc.)

Example: store a long message efficiently on AVR boards:

#include <avr/pgmspace.h>

const char msg[] PROGMEM = "This is a long message stored in Flash.";

void setup() {
  Serial.begin(9600);
  char buf[64];
  strcpy_P(buf, msg);
  Serial.println(buf);
}
void loop() {}

15) low memory available, stability problems may occur

What it means

You’re close to exhausting SRAM (runtime memory). This can compile but crash later.

Fix strategies

  • Reduce global arrays
  • Avoid String (dynamic allocation)
  • Store constants in Flash (AVR)
  • Use smaller data types (byte, uint16_t) where appropriate

Example: reduce SRAM usage

// bad (2000 bytes on Uno)
int big[1000];

// better (1000 bytes)
byte big2[1000];

16) Library conflicts and duplicate headers

Symptom

You included a library that has multiple versions installed, or two libraries provide the same header/class name.

Typical compile output clues

  • “Multiple libraries were found for …”
  • Odd type conflicts in headers

Fix strategies

  • Remove the unused duplicate library folder
  • Prefer the official library version for your board family
  • Keep libraries updated consistently

17) C++ class mistakes (common with libraries)

expected initializer before ... / constructor issues

Example that fails:

class Motor {
  int pin;
  Motor(int p) { pin = p; } // constructor is private by default here
};

Motor m(9); // error

Fix: make constructor public

class Motor {
public:
  int pin;
  Motor(int p) : pin(p) {}
};

Motor m(9);

18) Macro name collisions (surprisingly common)

What it means

Some libraries or headers define macros like min, max, or round that collide.

Example that can fail in strange ways:

int max = 10; // can conflict with max() macro in some contexts

Fix: rename variables:

int maxValue = 10;

Debugging Workflow That Saves Time

  1. Fix the first error only, then recompile.
  2. If errors are huge, comment out blocks until it compiles.
  3. Reintroduce code chunk-by-chunk.
  4. If a library triggers errors, test a minimal sketch:
    #include <LibraryName.h>
    void setup() {}
    void loop() {}
    
  5. If a sketch compiled yesterday but fails today, suspect:
    • Board selection changed
    • Library updated
    • IDE/toolchain updated

Mini “Error to Fix” Cheat Sheet

  • expected ';' → add missing semicolon
  • expected '}' → close braces
  • not declared in this scope → define in correct scope / check spelling
  • redefinition → remove duplicate definition
  • multiple definitionextern in header, define in one .cpp
  • undefined reference → missing implementation or missing file in build
  • no such file or directory → install library / correct header
  • no matching function → wrong arguments
  • sketch too big → reduce Flash usage / change board
  • low memory → reduce SRAM usage / avoid String

 

Share
[lebox id="2"]

You may also like