Arduino草图,多定义错误

Posted

技术标签:

【中文标题】Arduino草图,多定义错误【英文标题】:Arduino sketch, multiple definition error 【发布时间】:2015-06-27 22:29:31 【问题描述】:

我正在尝试将我创建的以前工作的草图分解成一个类和草图,但我遇到了一些麻烦。我正在扩展为我正在开发的板设计的键盘类的功能,但编译器一直抱怨函数的多个定义。

这是我的草图、我的 .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.

我做错了什么?

【问题讨论】:

看起来有些源文件被复制了,因此被编译了两次,例如C:\Users\Dday\Documents\Arduino\libraries\MacroKeyboard/MacroKeyboard.cppC:\Users\Dday\AppData\Local\Temp\build538426719655717639.tmp/MacroKeyboard.cpp 啊哎呀,当我试图将它粘贴到我的库文件夹中时,这是多个包含内容。修复后,我仍然遇到 DigiKeyboard 库的问题:/ 上面编辑的帖子出现了新错误 您必须添加所有相关代码:我在您发布的代码中没有看到usbFunctionSetupDigiKeyboardusbDescriptorHidReport 的定义。 抱歉,这些文件位于我拥有的 arduino 构建中包含的 Digikeyboard 库中。 github链接在这里:github.com/digistump/DigisparkArduinoIntegration/blob/master/… 【参考方案1】:

您使用的库以一种奇怪的方式编写。在文件DigiKeyboard.h 中有这样的定义(cmets mine):

// 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 文件中来使用它。

【讨论】:

啊,有道理。感谢您的所有帮助!

以上是关于Arduino草图,多定义错误的主要内容,如果未能解决你的问题,请参考以下文章

合并 Arduino 草图 (MIDI)

new-Operator 大幅增加了 Arduino 草图的大小 - 为啥?

c_cpp Arduino草图通过色谱循环RGB LED。

Arduino Yun中的闪存问题

Arduino 上的模拟读数返回错误值

通过 Processing/Arduino 控制 Spotify