My custom library won't work, It says: X was not declared in this scope

This error is probably near the top of the list for most common problems, it is quite generic as there are many ways to trigger this failure, especially since the Arduino IDE introduces its own subtle problems, which ironically are caused by the systems put in place to make life easier for new users.

FAQ_LIB_TEST.cpp:5: error: 'SPI' was not declared in this scope

Above is a typical example of this error generated by avr-gcc[1]. There are a number of reasons why this error is encountered and below is an explanation of the most common scenarios.

  • The identifer is spelled incorrectly

    It is easy to overlook the spelling of an identifier. Remember that C++ is also case sensitive, which leads me to point out, do not use identifiers spelled the same but using a different case as it can only lead to cunfusion.

  • The identifier is really 'not in scope'

    When a spelling mismatch is not the culprit, the error can usually be put down to a scoping error, however this can manifest itself in a number of ways.

    •  The file containing the declaration for the identifier has not been included.
    • The declaration is inside a scope like a namespace or class.
    • The identifier has not been declared.

When applying the error to the Arduino IDE, there are a few specific cases where the true cause of this error isn't apparent. This can be in part due to how the IDE changes your code before compilation. An Arduino specific version of this error is explained here: The library is in the right folder but it does not work.

Tag notes.

  1. Avr-gcc is the name of the compiler collection used by the Arduino IDE. The standard C implementation is called Avr-libc, which lives here:
Tags: libraries
Last update:
2014-03-02 01:47
Christopher Andrews
Average rating: 5 (1 Vote)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.