user1012032
user1012032

Reputation:

Multiple definition of several functions in two different .o files.

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>

Upvotes: 0

Views: 2181

Answers (3)

sub_o
sub_o

Reputation: 2802

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.

Upvotes: 0

W.B.
W.B.

Reputation: 5525

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.

Upvotes: 1

Arun
Arun

Reputation: 2102

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

Upvotes: 0

Related Questions