0
votes

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 ofwriteMotors()' 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 function writeMotors()' Motors_PWM_Timer.h:multiple definition ofmotorCommand' AeroQuad.cpp.o:(.bss.motorCommand+0x0): first defined here testMotor.cpp.o:(.data.numberOfMotors+0x0): multiple definition of numberOfMotors' AeroQuad.cpp.o:(.data.numberOfMotors+0x0): first defined here testMotor.cpp.o:In functioncommandAllMotors(int)' Motors_PWM_Timer.h:multiple definition of commandAllMotors(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 of initializeMotors(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 functioninitMotors(NB_Motors)' testMotor.h:multiple definition of initMotors(NB_Motors)' AeroQuad.cpp.o:\testMotor.h:22: first defined here testMotor.cpp.o:In functionpulseMotors(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:

enter image description here

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?

1
Try to keep declarations in header files and definitions in .cpp files.Biffen
Well you do define initMotors in the header file, so each source file (translation unit) where you include the header file it will be defined.Some programmer dude
For now you could declare initMotors to be inline to work around this, but in general, as others have said, you should put function definitions in your .c/.cpp files.Paul R
@JoachimPileborg I have initMotors in the setup() like this: initMotors(NB_MOTOR_CONFIG);AdiT
That's how you call the function, but it's still defined in the header file. When, how and where you call it doesn't matter, only where it's defined.Some programmer dude

1 Answers

0
votes

This looks like a library called Motors_PWM_Timer.h attempts to define functions that you are also defining in your testMotor.h

More Info

When Visual Micro compiles it should do what the Arduino IDE does, which means the Visual Studio project is not used. The Arduino IDE can only look at the actual source files on disk so this is how things are compiled in both Arduino and Visual Micro.

Visual Micro uses the same tool chain that the Arduino IDE uses.

The Visual Studio project is purely for ease of use and to power intellisense.

The best place for Visual Micro help is in the dedicated forum.

Thanks