Switch to full style
Understanding the language, error messages, etc.
Post a reply

Using a library in a... library

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 ?

Re: Using a library in a... library

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?

Re: Using a library in a... library

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:
#ifndef _LIBRARYH_ //avoid including the header several times
#define _LIBRARYH_
#include "Arduino.h"
#include <SPI.h>
//your code here
#endif


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


Program.ino
Code:
#include <Library.h>
#include <SPI.h>
//your code here

Re: Using a library in a... library

Mon Mar 28, 2016 8:25 pm

Ah, that's it.
Code:
#ifndef _LIBRARYH_ //avoid including the header several times
#define _LIBRARYH_

Knew I forgot about something
;)
Post a reply