Using a library in a... library

Understanding the language, error messages, etc.

Using a library in a... library

Postby 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 ?
Risike
 
Posts: 24
Joined: Mon Sep 14, 2015 5:25 pm

Re: Using a library in a... library

Postby 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?
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: Using a library in a... library

Postby 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
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: Using a library in a... library

Postby 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
;)
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 20 guests

cron