1

I am creating my own library for the Arduino and I have the following code:

#include <Arduino.h>
#include <Servo.h>

class ServoMotorController {
    public:
        ServoMotorController(int servoPin, int motorPin, int angle);
        void setAngle(int angle);
        void setSpeed(int speed);
    private:
        Servo servo;

        int servoPin;
        int servoAngle;
        int motorPin;
        int motorSpeed;
};

I am getting the error that Servo does not name a type; however if I do

#include <Servo.h>
#include <ServoMotorController.h>

in my actual Arduino code then I do not get the error. Why is the include statement not working in my C++ lib?

  • 2
    Where are the files located? There is an important distinction between #include <File.h> and #include "File.h". – David May 18 '14 at 17:41
  • There is a folder called "libraries" in them there are (for our purposes) 2 folders "ServoMotorController" (which has my files) and "Servo" (which has the servo files) – Jister13 May 18 '14 at 17:42
  • Check out the include path that is configured for your pre-processor/compiler. It is quite possible the folders aren't included. – David May 18 '14 at 17:44
  • Here is the thing. When I do include "" it says the file deosn't exist. But when I do include <> no such error occurs. Ontop of that the file is actually called SoftwareServo.h but the arduino software just include Servo.h... – Jister13 May 18 '14 at 17:45
  • It seems this question is similar to arduino.stackexchange.com/questions/322/… I suggest you take a look at it, it may provide you with the answer you're looking for. – jfpoilpret May 18 '14 at 18:45
1

This is a symptom of the arduino IDE. libraries can't add other libraries to the include path, but the can use other libs if they have already been included. you can use an error directive so your compiler output is more informative at least

#ifndef Servo_h
    #error "ServoMotorController Lib requires Servo.h to be included first to function"
#else

Arduino 1.5.6 beta's new library format has a section for dependencies in the new library.properties file that should allow one library to add another to the include path, but I'm not entirely sure if the functionality is fully implemented yet (its beta after all).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.