При работе с Arduino (VSCode и PlatformIO) я получаю несколько предупреждений multiple definition of xxx
во время компиляции, которые приводят к следующей ошибке:
collect2: error: ld returned 1 exit status
*** [.pio/build/nanoatmega328new/firmware.elf] Error 1
Поскольку class Motor
, кажется, уже определено в Arduino.h
, мне пришлось создать namespace motor
, и мне интересно, является ли это источником проблемы.
Вот мои файлы c ++ / h:
main.cpp
:
#include "Arduino.h"
#include "Motor.h"
using namespace motor;
const int pin1Left = 5;
const int pin2Left = 6;
const int pin1Right = 9;
const int pin2Right = 10;
Motor leftMotor(pin1Left, pin2Left);
Motor rightMotor(pin1Right, pin2Right);
void setup() {
}
void loop() {
leftMotor.forward();
rightMotor.forward();
delay(500);
}
Motor.h
:
#include "Enum.h"
namespace motor{
class Motor {
public:
Motor(int pin1, int pin2);
void backward();
void forward();
void setSpeed(Speed spd);
void reverse();
private:
Speed speed;
Direction dir;
int pin1;
int pin2;
};
}
Motor.cpp
:
#include "Arduino.h"
#include "Motor.h"
#include "Enum.h"
using namespace motor;
Motor::Motor(int pin1, int pin2){
pin1 = pin1;
pin2 = pin2;
dir = Direction::FORWARD;
speed = Speed::SLOW;
pinMode(pin1, INPUT);
pinMode(pin2, INPUT);
}
void Motor::backward(){
dir = Direction::BACKWARD;
analogWrite(pin1, static_cast<int>(speed));
analogWrite(pin2, 0);
}
void Motor::forward(){
dir = Direction::FORWARD;
analogWrite(pin1, 0);
analogWrite(pin2, static_cast<int>(speed));
}
void Motor::setSpeed(Speed spd){
speed = spd;
if (dir==Direction::BACKWARD){
backward();
} else{
forward();
}
}
void Motor::reverse(){
if (dir==Direction::BACKWARD){
forward();
} else{
backward();
}
}
И полный журнал ошибок:
Linking .pio/build/nanoatmega328new/firmware.elf
.pio/build/nanoatmega328new/src/main.cpp.o (symbol from plugin): In function `motor::Motor::Motor(int, int)':
(.text+0x0): multiple definition of `motor::Motor::Motor(int, int)'
.pio/build/nanoatmega328new/src/Motor.cpp.o (symbol from plugin):(.text+0x0): first defined here
.pio/build/nanoatmega328new/src/main.cpp.o (symbol from plugin): In function `motor::Motor::Motor(int, int)':
(.text+0x0): multiple definition of `motor::Motor::Motor(int, int)'
.pio/build/nanoatmega328new/src/Motor.cpp.o (symbol from plugin):(.text+0x0): first defined here
.pio/build/nanoatmega328new/src/main.cpp.o (symbol from plugin): In function `motor::Motor::Motor(int, int)':
(.text+0x0): multiple definition of `motor::Motor::backward()'
.pio/build/nanoatmega328new/src/Motor.cpp.o (symbol from plugin):(.text+0x0): first defined here
.pio/build/nanoatmega328new/src/main.cpp.o (symbol from plugin): In function `motor::Motor::Motor(int, int)':
(.text+0x0): multiple definition of `motor::Motor::forward()'
.pio/build/nanoatmega328new/src/Motor.cpp.o (symbol from plugin):(.text+0x0): first defined here
.pio/build/nanoatmega328new/src/main.cpp.o (symbol from plugin): In function `motor::Motor::Motor(int, int)':
(.text+0x0): multiple definition of `motor::Motor::setSpeed(Speed)'
.pio/build/nanoatmega328new/src/Motor.cpp.o (symbol from plugin):(.text+0x0): first defined here
.pio/build/nanoatmega328new/src/main.cpp.o (symbol from plugin): In function `motor::Motor::Motor(int, int)':
(.text+0x0): multiple definition of `motor::Motor::reverse()'
.pio/build/nanoatmega328new/src/Motor.cpp.o (symbol from plugin):(.text+0x0): first defined here
.pio/build/nanoatmega328new/src/main.cpp.o (symbol from plugin): In function `motor::Motor::Motor(int, int)':
(.text+0x0): multiple definition of `setup'
.pio/build/nanoatmega328new/src/debugHBridge.cpp.o (symbol from plugin):(.text+0x0): first defined here
.pio/build/nanoatmega328new/src/main.cpp.o (symbol from plugin): In function `motor::Motor::Motor(int, int)':
(.text+0x0): multiple definition of `loop'
.pio/build/nanoatmega328new/src/debugHBridge.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
*** [.pio/build/nanoatmega328new/firmware.elf] Error 1
============================ [FAILED] Took 2.47 seconds ============================
РЕДАКТИРОВАТЬ - После замены #include "Motor.cpp"
на #include "Motor.h"
я все равно получаю:
Linking .pio/build/nanoatmega328new/firmware.elf
.pio/build/nanoatmega328new/src/main.cpp.o (symbol from plugin): In function `leftMotor':
(.text+0x0): multiple definition of `setup'
.pio/build/nanoatmega328new/src/debugHBridge.cpp.o (symbol from plugin):(.text+0x0): first defined here
.pio/build/nanoatmega328new/src/main.cpp.o (symbol from plugin): In function `leftMotor':
(.text+0x0): multiple definition of `loop'
.pio/build/nanoatmega328new/src/debugHBridge.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
*** [.pio/build/nanoatmega328new/firmware.elf] Error 1
============================ [FAILED] Took 2.42 seconds ============================