Error with library

Hello, I was trying to write a “Utility” library where I could include various functions and classes for utility in other projects. So I tried to test my idea out with a Vector2D class, and this is the Utilities.h file:

#ifndef utils_h
#define utils_h

#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#include <WProgram.h>

#include "Vector2D\Vector2D.h"

#endif // utils_h

And when I try to do something like this:

#include <Utilities.h>

Vector2D test(3,4);

void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly:


This code doesn’t compile. However, when I compile it without the test object it compiles fine.
When I try to compile it with the object I get this error:

C:\Users\(USER)\AppData\Local\Temp\ccAbGcab.ltrans0.ltrans.o: In function `__static_initialization_and_destruction_0':

C:\Users\DESPAC~1\AppData\Local\Temp\arduino_modified_sketch_344082/sketch_dec28a.ino:3: undefined reference to `Vector2D::Vector2D(float const&, float const&)'

collect2.exe: error: ld returned 1 exit status

Multiple libraries were found for "Utilities .h"
 Used: C:\Users\(USER)\Documents\Arduino\libraries\Utilities 
Using library Utilities in folder: C:\Users\(USER)\Documents\Arduino\libraries\Utilities (legacy)
exit status 1
Error compiling for board Arduino Uno.

But I dont get this error if I copy the Vector2D code to the .ino file.

My folder structure looks like this:

- //other libraries
- Utilities
- - src
- - - Vector2D.h
- - - Vector2D.cpp
- - keywords.txt
- - Utilities.h

Thank you!

I suspect the problem is the folder structure of your library. Recursive compilation of subfolders of the library is only done when the library is in the 1.5 Arduino library format, but your library is in the 1.0 format. For more information, see:

So try making your library conform to either the 1.0 or 1.5 formats following the information at the link above. If that doesn't fix the problem, post your full library and I'll take a closer look.

Thanks for the help! I followed the folder structure for the library and i started working perfectly. However, when I tried to use functions like abs or cos in the file, when I try to compile the file I get an error which says that those functions are undeclared.

The reason why you can use these functions in a sketch without getting that error is because the Arduino IDE automatically adds an #include directive for Arduino.h to the .ino file of a sketch. It doesn't do that for files with other extensions. So you need to add the #include directive to the code yourself.

abs() is declared in Arduino.h. cos() is declared in math.h, but math.h is #included when you #include Arduino.h so you get both of them that way.