Arduino草图,多重定义错误

时间:2015-06-27 22:29:31

标签: c++ arduino multiple-definition-error

我试图打破我在班级和草图中创建的以前工作的草图,我遇到了一些麻烦。我正在扩展为我正在使用的电路板设计的键盘类的功能,但编译器一直抱怨函数的多个定义。

这是我的草图,我的.h和我的.cpp:

草图

#include <DigiKeyboard.h>
#include "MacroKeyboard.h"

//constants
#define LED 1
#define SWITCH 0


//configuration
#define PAUSE_ON_ENTER true

const char command[] PROGMEM = "test string";
MacroKeyboard kb(command, SWITCH);

void setup() {
  //tie switch to high 
  pinMode(SWITCH, OUTPUT);
  digitalWrite(SWITCH, HIGH);

  // set pins to correct uh whatever
  pinMode(LED, OUTPUT);
  pinMode(SWITCH, INPUT);

}

void loop() {
  DigiKeyboard.update();
  kb.update();
}

·H:

#ifndef MacroKeyboard_h
#define MacroKeyboard_h

#include "Arduino.h"

#define ON LOW
#define OFF HIGH
#define SCANCODE_DIFF 8

class MacroKeyboard {
    const char* command;
    int pin;
    bool isPressed = false;

  public:
    MacroKeyboard(const char*, int);
    void parseLetter(int);
    void update();
};
#endif

的.cpp:

#include <DigiKeyboard.h>
#include "MacroKeyboard.h"

MacroKeyboard::MacroKeyboard(const char* macro_position, int pin_number){
  command = macro_position;
  pin = pin_number;
}

void MacroKeyboard::parseLetter(int letter){
  unsigned int scancode = pgm_read_byte_near(ascii_to_scan_code_table + letter - SCANCODE_DIFF);
  unsigned int keycode = scancode & ((1 << 7) - 1); // masking off shift bit

  unsigned int mods = 0;
  unsigned int shift = (scancode & (1 << 7)) >> 7;
  mods |= (shift == 1) ? MOD_SHIFT_LEFT : 0;

  DigiKeyboard.sendKeyStroke(keycode, mods);
}

void MacroKeyboard::update(){
  if(digitalRead(pin) == ON && !isPressed){
    isPressed = true;
    for (int i = 0; i < strlen(command); i++){
      char letter = pgm_read_byte_near(command + i);
      parseLetter(letter);
    }
  }

  if (digitalRead(pin) == OFF && isPressed){
    isPressed = false; // debounce
  }
}

我得到的错误是:

    MacroKeyboard\MacroKeyboard.cpp.o: In function `usbFunctionSetup':
    C:\Program Files (x86)\Digistump_Arduino\hardware\digistump\avr\libraries\DigisparkKeyboard/DigiKeyboard.h:216: multiple definition of `usbFunctionSetup'
    MacroKeyboardTest.cpp.o:C:\Program Files (x86)\Digistump_Arduino\hardware\digistump\avr\libraries\DigisparkKeyboard/DigiKeyboard.h:216: first defined here
    MacroKeyboard\MacroKeyboard.cpp.o: In function `MacroKeyboard::MacroKeyboard(char const*, int)':
    C:\Program Files (x86)\Digistump_Arduino\hardware\digistump\avr\libraries\DigisparkKeyboard/DigiKeyboard.h:170: multiple definition of `DigiKeyboard'
    MacroKeyboardTest.cpp.o:C:\Program Files (x86)\Digistump_Arduino\hardware\digistump\avr\libraries\DigisparkKeyboard/DigiKeyboard.h:199: first defined here
    MacroKeyboard\MacroKeyboard.cpp.o:(.progmem.data.usbDescriptorHidReport+0x0): multiple definition of `usbDescriptorHidReport'
    MacroKeyboardTest.cpp.o:(.progmem.data.usbDescriptorHidReport+0x0): first defined here
    collect2: error: ld returned 1 exit status
    Error compiling.

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您使用的库是以奇怪的方式编写的。在文件DigiKeyboard.h中有如(我的评论)的定义:

// Declares a global variable.
// If you include this file in more than one cpp file it will cause linker error.
DigiKeyboardDevice DigiKeyboard = DigiKeyboardDevice();

// Defines a non-inline function
// Again, multiple definitions error if the file is included in several source files.
uchar usbFunctionSetup(uchar data[8]) {
    // code
}

所以你必须通过将这个.h文件只包含在一个.cpp文件中来使用它。