Arduino库中的多个定义错误
Posted
技术标签:
【中文标题】Arduino库中的多个定义错误【英文标题】:Multiple definition errors in Arduino library 【发布时间】:2013-12-27 11:47:56 【问题描述】:我正在尝试在 Arduino 1.0.5 中从 https://github.com/janisHD/LightRobot 编译 lightRobot.ino Arduino 草图,但我遇到了 lightRobot 库中函数的多个定义错误。 我使用的是 OS X 10.8.5,但在 Windows 8 中遇到了同样的问题。
以下是 .h 和 .cpp 文件示例:
BlueToothEvent.h
/*! \file BlueToothEvent.h checks periodically if new data over BT has been received.
*/
#include <Arduino.h>
#include <OrangutanLCD.h>
#include "TimeEvent.h"
#ifndef BLUETOOTH_EVENT_H
#define BLUETOOTH_EVENT_H
#define DATA_WORD_LENGTH 4
/*! \class BlueToothEvent
retrieves and stores the received BT data. The data should come over the serialport, it must be started in "setup".
The struct DataPacket publishes the parsed data for the Statemanager.
*/
class BlueToothEvent : public TimeEvent
public:
struct DataPacket
char speed;
char direction;
int color[4]; // [3]==blue [2]==green [1]==red [0]==brightness
int mode[2]; // [1]==color mode (0000->remote, 0001->blink, 0010->random, 0011->random&blink) [0]==drive mode (0000->remote, 0001->random)
;
BlueToothEvent();
~BlueToothEvent();
/*! Callback which is executed periodically*/
virtual void onTimeEvent();
/*! Returns an internal state.*/
virtual unsigned char getInternalState();
/*! Sets an internal state.*/
virtual void setInternalState(unsigned char state, bool update=false);
/*! Executes a more complex (and time consuming) action.*/
virtual void executeAction();
/*! To get the received data in the DataPacket struct
\return The most recent data received via Serial connection
*/
DataPacket getDataPacket();
bool m_new_data_present;
private:
/*! Processes the array with a 4 byte data word and saves the information in the DataPacket field.
\param data the array (must have the length of 4 bytes)
*/
void processData(unsigned char* data);
private:
enum Data
velocity=0,//desired velocity
direction,//direction to drive
color,//desired color of the LEDs
mode,//the different modes: remote, random, blink
;
unsigned char m_data[DATA_WORD_LENGTH];
struct DataPacket m_data_packet;
;
#endif
BlueToothEvent.cpp
#include "BlueToothEvent.h"
BlueToothEvent::BlueToothEvent():
TimeEvent(),
m_new_data_present(false)
//init data array
m_data[velocity] = 0;
m_data[direction] = 0;
m_data[color] = 0;
m_data[mode] = 0;
processData(m_data);
//Serial connection
void BlueToothEvent::onTimeEvent()
//Code to receive a single data word and store it in m_data field
//Word consists of 4 chars (see the docu for further explanations):
//[0] -> direction to drive [0-254]
//[1] -> velocity to drive [0-254]
//[2] -> desired color for the Light [0-254] in 2 bit packets -> b0[0,3]->Brightnes | [0,3]->Red| [0,3]->Green | [0,3]->Blue
//[3] -> internal mode (see responsible class)[0-254]
if(Serial.available() >= DATA_WORD_LENGTH)
//minimum number of bytes must be available in the buffer
while(Serial.available() > DATA_WORD_LENGTH)
Serial.read();//clear buffer except the last 4 bits
m_data[velocity] = (char)Serial.read();
m_data[direction] = (char)Serial.read();
m_data[color] = (char)Serial.read();
m_data[mode] = (char)Serial.read();
processData(m_data);
m_new_data_present = true;
void BlueToothEvent::processData(unsigned char* data)
m_data_packet.speed = data[velocity];
m_data_packet.direction = data[direction];
m_data_packet.color[0] = data[color] & 0b00000011;
m_data_packet.color[1] = (data[color] & 0b00001100)>>2;
m_data_packet.color[2] = (data[color] & 0b00110000)>>4;
m_data_packet.color[3] = (data[color] & 0b11000000)>>6;
m_data_packet.mode[0] = data[mode] & B00001111;
m_data_packet.mode[1] = (data[mode] & B11110000)>>4;
BlueToothEvent::DataPacket BlueToothEvent::getDataPacket()
m_new_data_present = false;
return m_data_packet;
//unsigned char BlueToothEvent::getData(unsigned char field)
//
// if(field <= mode)
// return m_data[field];
// else
// return 0;
//
unsigned char BlueToothEvent::getInternalState()
return m_data[mode];
void BlueToothEvent::setInternalState(unsigned char state, bool update)
//nothing to do here!
void BlueToothEvent::executeAction()
//nothing to do here!
我在编译时收到以下错误 - 我只显示了列出的 .h/.cpp 文件对的错误 - lightRobot 库中的每个文件都有类似的错误:
lightRobot/BlueToothEvent.cpp.o: In function `BlueToothEvent::processData(unsigned char*)':
/Users/lemmy/Documents/Arduino/libraries/lightRobot/BlueToothEvent.cpp:43: multiple definition of `BlueToothEvent::processData(unsigned char*)'
BlueToothEvent.cpp.o:BlueToothEvent.cpp:43: first defined here
lightRobot/BlueToothEvent.cpp.o: In function `BlueToothEvent':
/Users/lemmy/Documents/Arduino/libraries/lightRobot/BlueToothEvent.cpp:4: multiple definition of `BlueToothEvent::BlueToothEvent()'
BlueToothEvent.cpp.o:BlueToothEvent.cpp:4: first defined here
lightRobot/BlueToothEvent.cpp.o: In function `BlueToothEvent':
/Users/lemmy/Documents/Arduino/libraries/lightRobot/BlueToothEvent.cpp:4: multiple definition of `BlueToothEvent::BlueToothEvent()'
BlueToothEvent.cpp.o:BlueToothEvent.cpp:4: first defined here
lightRobot/BlueToothEvent.cpp.o: In function `BlueToothEvent::getDataPacket()':
/Users/lemmy/Documents/Arduino/libraries/lightRobot/BlueToothEvent.cpp:56: multiple definition of `BlueToothEvent::getDataPacket()'
BlueToothEvent.cpp.o:BlueToothEvent.cpp:56: first defined here
lightRobot/BlueToothEvent.cpp.o: In function `BlueToothEvent::getInternalState()':
/Users/lemmy/Documents/Arduino/libraries/lightRobot/BlueToothEvent.cpp:73: multiple definition of `BlueToothEvent::getInternalState()'
BlueToothEvent.cpp.o:BlueToothEvent.cpp:73: first defined here
lightRobot/BlueToothEvent.cpp.o: In function `BlueToothEvent::setInternalState(unsigned char, bool)':
/Users/lemmy/Documents/Arduino/libraries/lightRobot/BlueToothEvent.cpp:78: multiple definition of `BlueToothEvent::setInternalState(unsigned char, bool)'
BlueToothEvent.cpp.o:BlueToothEvent.cpp:78: first defined here
lightRobot/BlueToothEvent.cpp.o: In function `BlueToothEvent::executeAction()':
/Users/lemmy/Documents/Arduino/libraries/lightRobot/BlueToothEvent.cpp:83: multiple definition of `BlueToothEvent::executeAction()'
BlueToothEvent.cpp.o:BlueToothEvent.cpp:83: first defined here
lightRobot/BlueToothEvent.cpp.o: In function `BlueToothEvent::onTimeEvent()':
/Users/lemmy/Documents/Arduino/libraries/lightRobot/BlueToothEvent.cpp:18: multiple definition of `BlueToothEvent::onTimeEvent()'
BlueToothEvent.cpp.o:BlueToothEvent.cpp:18: first defined here
似乎错误发生在目标文件中,但我不确定为什么,因为头文件有保护它们以防止多个定义。
【问题讨论】:
它使您将编译的源文件与旧编译的源文件链接起来。你在构建之前尝试过清理吗? @LS_dev 我不确定如何在 Arduino 中做到这一点 - 我相信它是建立在 C++ 编译器之上的,但它只是提供了“验证/编译”选项,这就是我的一直在做。 它将您的代码与您自己的代码链接起来(两次)。不知何故,您应该再次将中间文件 (.o
) 指定为输入文件。
建议:新建一个项目,添加已有的BlueToothEvent.h
和BlueToothEvent.cpp
。
【参考方案1】:
万一其他人后来遇到这个问题,我也遇到了同样的问题,并且通过删除我草图中库文件上的选项卡来解决这个问题。当您拥有 #include "lib.h"
【讨论】:
【参考方案2】:当我将 .ino 文件移出 lightRobot 文件夹时,编译成功。
【讨论】:
传奇!这为我修好了。谢谢。【参考方案3】:我知道您已经找到了答案,但是对于未来的搜索者,我遇到了同样的问题。我通过为 .ino 重命名主文件夹并从文件夹中删除所有 .h 或 .cpp 文件来修复它。
注意:我不知道这是怎么发生在我身上的,但这可能是其他人的问题。
【讨论】:
以上是关于Arduino库中的多个定义错误的主要内容,如果未能解决你的问题,请参考以下文章
Arduino下LCD1602综合探究(中)——如何减少1602的连线,LiquidCrystal库,LiquidCrystal库中bug的解决方法