GamebuinoClassic archive

Archive of the Gamebuino Classic forum (2014-2018), rebuilt from the pages kept by the Wayback Machine: only part of the topics and pages were captured. Read-only. Removal on request: hi@gamebuino.com.

Forum › Programming Questions

Using a library in a... library

Risike Tue Nov 03, 2015 10:33 am
Hi everybody,

I need to write an Arduino Library (in the Libraries folder) which uses others libraries.
For example I need to use SPI.h in my own library.

So, in my main sketch (ino file) I #include <SPI.h>
And in my library header (Mylib.h) I #include "SPI.h" to use some functions.
And in my library CPP file (Mylib.cpp) I #include "Mylib.h".
Then, I include <Mylib.h> in my main sketch (ino file).

But when I compile, it said "multiple definitions of SPI.h" (sorry, not the exact message, I'm not at home).
I've searching hours on internet to find a solution but nothing... I've studied the Gamebuino Library which do the same and it works.
What the problem please ?
Montiey Sat Mar 26, 2016 7:03 pm
I can't remember, but I heard that there is a trick to solve this problem. If you call SPI in the sketch, and also call your Library, then when the Library calls SPI you'll get an error. Try removing all but one definition?
rodot Mon Mar 28, 2016 1:00 pm
It works for me when I do the following (it's how it's done with the Gamebuino library):

Library.h
Code: Select all
#ifndef _LIBRARYH_ //avoid including the header several times
#define _LIBRARYH_
#include "Arduino.h"
#include <SPI.h>
//your code here
#endif


Library.cpp
Code: Select all
#include <Library.h>
#include <SPI.h>
//your code here


Program.ino
Code: Select all
#include <Library.h>
#include <SPI.h>
//your code here
Montiey Mon Mar 28, 2016 8:25 pm
Ah, that's it.
Code: Select all
#ifndef _LIBRARYH_ //avoid including the header several times
#define _LIBRARYH_

Knew I forgot about something
;)