Go Down

Topic: Using one library in another (Read 9612 times) previous topic - next topic

huckw

I have a class I am defining in a set of .h and .cpp files along with my sketch.  I might make it a library eventually, but for now I have it in the same folder as my pde file.  In the .h file I have this at the top...

#include <ARDUINOINSTALLPATH\libraries\TimerOne\TimerOne.h>

And inside my class definition I make an instance of TimerOne like so...

TimerOne Timer1;

I get compile errors when trying to use any method in TimerOne.

"undefined reference to `TimerOne::initialize(long)'"

Am I allowed to do this?  If so, can you help me with what I'm doing wrong?

I'm on a windows Vista machine, using Arduino 0021.  I have successfully used the TimerOne library on its own but now that I'm organizing my code into a class defined in a .h and .cpp I'm having issues.

Thanks in advance

PaulS

Quote
Am I allowed to do this?

Yes, you are. But...

You knew there was going to be a but, didn't you?

What gets compiled and linked? Only the sketch and any source files referenced by including headers in the main sketch.

If you have a class TimerWrapper that includes TimerOne, and a sketch that includes only TimerWrapper, the sketch and TimerWrapper will be compiled, but TimerOne will not be. Any references in TimerWrapper to functions in TimerOne will result in undefined external references, as you are seeing.

The solution, then, is to include TimerOne.h in the sketch, too.

huckw

Ok.  That makes sense.  Thanks for the quick reply.  

I've now included TimerOne.h in the main sketch (as well as TimerWrapper.h and now get a class redefinition error...

In file included from sketch_jan06a.cpp:2:
C:\Program Files (x86)\Arduino\Arduino-0021\libraries\TimerOne/TimerOne.h:19: error: redefinition of 'class TimerOne'
C:\Program Files (x86)\Arduino\Arduino-0021\libraries\TimerOne\TimerOne.h:20: error: previous definition of 'class TimerOne'

If I don't include it in TimerWrapper I get an error when I try to instantiate a TimerOne object that "TimerOne does not name a type".

One thing that seems a little funny is that TimerOne does not have a constructor...I'm assuming that a default constructor is provided by the compiler or something.  

Any ideas?

PaulS

TimerOne.h does not include multiple inclusion guard code.

Add
Code: [Select]
#ifndef TimerOne_H
#define TimerOne_H

at the top, and
Code: [Select]
#endif
at the bottom of the header file.

huckw

Amazing support PaulS!!  You nailed it.  I now see the glorious message "Done compiling."  Thanks a ton.

Now onto testing and debugging!!

Go Up