0

Basically I have lots of errors like these:

    IMU/IMU.cpp.o: In function `MPU6050::dmpInitialize()':
   Projects/Arduino/libraries/IMU/MPU6050_6Axis_MotionApps20.h:281: multiple definition of `MPU6050::dmpInitialize()'  
    Quadcopter.cpp.o:Projects/Arduino/libraries/IMU/MPU6050_6Axis_MotionApps20.h:281: first defined here

But im not sure how to solve this. I have lookes into several other similar questions but didnt fint any answer related to this code.


.ino

#include <Wire.h>
#include <IMU.h> 
IMU imuController;
void setup() {
  Wire.begin();
  imuController.init();
}

IMU.h

#include "MPU6050_6Axis_MotionApps20.h"

MPU6050_6Axis_MotionApps20.h

#include "I2Cdev.h"
#include "helper_3dmath.h"
#include "MPU6050.h"
#include <avr/pgmspace.h>

MPU6050.h

#include "I2Cdev.h"
#include <avr/pgmspace.h>
  • possible duplicate of [Repeated Multiple Definition Errors from including same header in multiple cpps](http://stackoverflow.com/questions/223771/repeated-multiple-definition-errors-from-including-same-header-in-multiple-cpps) – Peter Wood Mar 27 '13 at 08:23

3 Answers3

1

It might be because you header file is included multiple times. What you can do is define guards like this:

#ifndef SOMEVAR - *make sure the file is included only once in the current scope*
#define SOMEVAR
//Symbol definitions
#endif

or you could include #pragma once in your header file, if your compiler supports it.

W.B.
  • 5,445
  • 19
  • 29
  • Arduino uses GCC's C++ compiler – leppie Mar 27 '13 at 08:17
  • Header file of which file? and is SOMEVAR the name of the .h file with extension? –  Mar 27 '13 at 08:20
  • Header of the file whose symbols are included multiple Times (in your case, I'm guessing it's I2CDev.h"). And SOMEVAR is an arbitrary name of your choice. Best to call it similar to the include file or to functions you define to avoid confusion. – W.B. Mar 27 '13 at 08:34
  • @leppie: Even though I am not a fan of it, simply because the standard doesn't guarantee it, pragma once is available on GCC. – Sebastian Mach Mar 27 '13 at 08:38
  • Did you do a clean build after the changes? sometimes *.o files may not get regenerated due to improper cleanups by the compiler tool-sets. If such a thing happens then I would prefer to delete *.o files manually and build again! – Arun Mar 27 '13 at 08:46
  • Yes I have cleaned. Arduino IDE makes a clean build when changing the board type. Oh goooosh! Still the same. –  Mar 27 '13 at 08:58
  • Problem solived. The I2c lib has some problems with the Arduino 1.0.1. After upgrading to 1.0.4 problem is fixed. Thank you everybody. :) –  Mar 27 '13 at 09:03
  • -1. Scope guards work only within a single .o file, and not across .o files. The definition here is explicitly in multiple .o files. The underlying reason is that scope guards are handled by the preprocessor when compiling each .cpp file, and prevent that one .cpp file from including a .h file twice. – MSalters Mar 27 '13 at 09:19
0

As W.B suggested, you need include guard for every header file you define.

Something like Ex: Header.h

   #ifndef HEADER_H
   #define HEADER_H
   // Header stuff in here...  
   #endif
Arun
  • 2,087
  • 2
  • 20
  • 33
  • Thank you. If I import library A and B to my ino file, can A use functions and classes from B and viceversa? –  Mar 27 '13 at 08:25
  • Your ino file can call exported APIs from lib. We are not worried about whether some libA functions call libB functions or not, because it would be implementation details of libA functions. Libs as such are black-boxes providing some functionality. If there are any dependencies as such then libA vendor should make sure that all its dependencies(other libs, resources, so on) are shipped along with libA for the end-user. – Arun Mar 27 '13 at 08:42
0

This is 7 years way too late, but here's what I did

  1. In my own mpu_sensor.h file, I only included
#ifndef MPU_SENSOR_H
#define MPU_SENSOR_H

#include "MPU6050.h"
#include "helper_3dmath.h"
....
#endif

Note that I don't MPU6050_6Axis_MotionApps20, since most datatypes are

  1. In my mpu_sensor.cpp file, here's my includes:
#include "MPU6050_6Axis_MotionApps20.h"
#include "mpu_sensor.h"

Please note that MPU6050_6Axis_MotionApps20.h must be before my including my own header file.

It works now. I agree that the library itself should be updated, but it seems like the author is not updating for the past few years.

sub_o
  • 2,642
  • 5
  • 28
  • 41