I'm using Visual Studio 2013 with Visual micro plugin for arduino boards. I got errors when I create a .cpp file, like in the error list below:
Compiling 'AeroQuad' for 'Arduino Mega 2560 or Mega ADK' testMotor.cpp.o:In function
writeMotors()' Motors_PWM_Timer.h:multiple definition of
writeMotors()' AeroQuad.cpp.o:C:\Users\Adi\Documents\Drone\AeroQuad_v3.2\libraries\AQ_Motors\Motors_PWM_Timer.h:127: first defined here ld.exe:Disabling relaxation: it will not work with multiple definitions testMotor.cpp.o:In functionwriteMotors()' Motors_PWM_Timer.h:multiple definition of
motorCommand' AeroQuad.cpp.o:(.bss.motorCommand+0x0): first defined here testMotor.cpp.o:(.data.numberOfMotors+0x0): multiple definition ofnumberOfMotors' AeroQuad.cpp.o:(.data.numberOfMotors+0x0): first defined here testMotor.cpp.o:In function
commandAllMotors(int)' Motors_PWM_Timer.h:multiple definition ofcommandAllMotors(int)' AeroQuad.cpp.o:C:\Users\Adi\Documents\Drone\AeroQuad_v3.2\libraries\AQ_Motors\Motors_PWM_Timer.h:149: first defined here testMotor.cpp.o:In function
initializeMotors(NB_Motors)' Motors_PWM_Timer.h:multiple definition ofinitializeMotors(NB_Motors)' AeroQuad.cpp.o:C:\Users\Adi\Documents\Drone\AeroQuad_v3.2\libraries\AQ_Motors\Motors_PWM_Timer.h:68: first defined here testMotor.cpp.o:In function
initMotors(NB_Motors)' testMotor.h:multiple definition ofinitMotors(NB_Motors)' AeroQuad.cpp.o:\testMotor.h:22: first defined here testMotor.cpp.o:In function
pulseMotors(unsigned char)' Motors.h:multiple definition of `pulseMotors(unsigned char)' AeroQuad.cpp.o:C:\Users\Adi\Documents\Drone\AeroQuad_v3.2\libraries\AQ_Motors\Motors.h:51: first defined here Error creating .elf
I don't understand why that?
My header file is like that:
// testMotor.h
#ifndef _TESTMOTOR_h
#define _TESTMOTOR_h
/*Adi*/
//#define MOTOR_PWM
#define MOTOR_PWM_Timer
//#define MOTOR_APM
//#define MOTOR_I2C
#define NB_MOTOR_4
//#define NB_MOTOR_6
//#define NB_MOTOR_8
#if defined MOTOR_PWM_Timer
#include <Motors_PWM_Timer.h>
#endif
#if defined (NB_MOTOR_4)
#define NB_MOTOR 4
#define NB_MOTOR_CONFIG FOUR_Motors
#endif
void testMotor(int motor);
void initMotors(NB_Motors motorConfig);
#endif
In my testMotor.cpp file I'am including the testMotor.h
file. Like in the code below:
#include "testMotor.h"
void testMotor(int motor) {
for (int motorTrust = 1000; motorTrust < 1400; motorTrust += 10) {
motorCommand[motor] = motorTrust;
writeMotors();
delay(200);
}
motorCommand[motor] = 1000;
writeMotors();
}
void initMotors(NB_Motors motorConfig) {
initializeMotors(motorConfig);
}
I'm also including testMotor.h
file in my .ino project. Like in the code below:
#include "testMotor.h"
#include "Motors.h"
#include <EEPROM.h>
#include <Wire.h>
#include <GlobalDefined.h>
#include "AeroQuad.h"
#include "PID.h"
#include <AQMath.h>
#include <FourtOrderFilter.h>
My solution project is shown in the image below:
the fact is when I exclude the testMotor.cpp
from the project it runs without errors.
Could the reason be that visual micro didn't use C++ compiler, but C compiler? Than I shoud change the compiler to C?
Can someone help me to solve the issue?
.cpp
files. – BiffeninitMotors
in the header file, so each source file (translation unit) where you include the header file it will be defined. – Some programmer dudeinitMotors
to beinline
to work around this, but in general, as others have said, you should put function definitions in your .c/.cpp files. – Paul R