多重定义错误 - Arduino库

时间:2014-05-31 00:03:50

标签: c++ arduino

我正在为Arduino建立一个基本的电机库。我正在尝试使用草图 motordrivertest.ino 测试它,并且我继续得到我的整个马达类及其所有方法的多重定义错误。我想知道如何解决这些错误。 (我使用的是MAC OS X 10.9.3)

这是我的错误。

MotorDriver/motordriver.cpp.o: In function `Motor::getSpeed()':
/Users/pattyborgman/Documents/Arduino/libraries/MotorDriver/motordriver.cpp:49: multiple definition of `Motor::getSpeed()'
motordrivertest.cpp.o:/Users/pattyborgman/Documents/Arduino/libraries/MotorDriver/motordriver.cpp:49: first defined here
MotorDriver/motordriver.cpp.o: In function `Motor::setSpeed(int)':
/Users/pattyborgman/Documents/Arduino/libraries/MotorDriver/motordriver.cpp:33: multiple definition of `Motor::setSpeed(int)'
motordrivertest.cpp.o:/Users/pattyborgman/Documents/Arduino/libraries/MotorDriver/motordriver.cpp:33: first defined here
MotorDriver/motordriver.cpp.o: In function `Motor':
/Users/pattyborgman/Documents/Arduino/libraries/MotorDriver/motordriver.cpp:15: multiple definition of `Motor::Motor(char)'
motordrivertest.cpp.o:/Users/pattyborgman/Documents/Arduino/libraries/MotorDriver/motordriver.cpp:15: first defined here
MotorDriver/motordriver.cpp.o: In function `Motor':
/Users/pattyborgman/Documents/Arduino/libraries/MotorDriver/motordriver.cpp:15: multiple definition of `Motor::Motor(char)'
motordrivertest.cpp.o:/Users/pattyborgman/Documents/Arduino/libraries/MotorDriver/motordriver.cpp:15: first defined here

这是我的草图,.h文件和.cpp文件。

motordrivertest.ino

#include "motordriver.h"
#include "motordriver.cpp"

Motor motor('a');

void setup(){}

void loop()
{
 motor.setSpeed(125);
 delay(3000);
 motor.setSpeed(-125);
 delay(3000); 
}

motordriver.h

#ifndef motordriver_h
#define motordriver_h

#include "Arduino.h"

class Motor
{
public:
    Motor(char letter);
    void setSpeed(int speed);
    int getSpeed();
private:
    int currentSpeed;
    int PWM;
    int BRK;
    int DIR;

};

#endif

motordriver.cpp

#include "Arduino.h"
#include "motordriver.h"

Motor::Motor(char letter)
{
        if (letter=='a') {
            int PWM = 3;
            int BRK = 9;
            int DIR = 12;
            pinMode(PWM, OUTPUT);
            pinMode(BRK, OUTPUT);
            pinMode(DIR, OUTPUT);
        } else {
            int PWM = 11;
            int BRK = 8;
            int DIR = 13;
            pinMode(PWM, OUTPUT);
            pinMode(BRK, OUTPUT);
            pinMode(DIR, OUTPUT);
        }

}

void Motor::setSpeed(int speed)
{
    currentSpeed=speed;
    if (speed >= 0) {
        analogWrite(PWM, speed);
        digitalWrite(BRK, LOW);
        digitalWrite(DIR, HIGH);
    }else {
        int _speed = -speed;
        analogWrite(PWM, _speed);
        digitalWrite(BRK, LOW);
        digitalWrite(DIR, LOW);
    }

}

int Motor::getSpeed()
{
    return currentSpeed;
}

1 个答案:

答案 0 :(得分:3)

避免这类错误的简单规则:

  • 从不#include .cpp文件
  • 如果在头文件中实现函数或方法,请在它们前面写入内联(不包括模板,默认情况下它们是内联的)

C(++) - 源代码编译成目标文件。对于每个.cpp文件,将生成一个目标文件。这些文件将包含函数/方法的机器代码。包含源文件将在多个目标文件中提供相同的功能。编译器没问题。编译器运行后,链接器将完成将所有目标文件和静态库组合成可执行文件的工作。现在它找到了几个具有相同名称的函数。那该怎么办?它通知您遇到的错误并退出。