9 years, 6 months ago.

Including I2C more then once

Hey,

I have some basic problems with my code. The compiler seems to have a problem with multiple instances of the I2C-Class.

Interface to I2C

#ifndef wire_h
#define wire_h

#include "mbed.h"

I2C i2c(D14,D15);

class wire{
public:
    wire();
    void set_frequency(int);
    bool read(int,int);
    bool write(int);
    void addBuffer(char*);
    char* getBuffer(void);
private:
    char* buffer; 
    int freq; 
    
};

#endif 

Sensor Class

#ifndef sparkfun_h
#define sparkfun_h

#include "mbed.h"
#include "wire.h"

#define GYRO 0x68                        // gyro I2C address
#define REG_GYRO_X 0x1D                  // IMU-3000 Register address for GYRO_XOUT_H
#define ACCEL 0x53                       // Accel I2c Address
#define ADXL345_POWER_CTL 0x2D

class sparkfun{
public:
    sparkfun(wire*);
    void read_sensor();
    int* get_gyro();
    int* get_accel();
    
    
private:
    
    wire* Wire;
    int* gyro;
    int* accel;
    char data[6];
    
};    
#endif

Main Class

#ifndef main_h
#define main_h
#include "mbed.h"
#include "filter.h"
#include "sparkfun.h"

#define RAD_TO_DEG 57.2957795

#endif

At compile I get two Errors:

Error: Symbol i2c multiply defined (by sparkfun.cpp.K64F.o and wire.cpp.K64F.o).

Error: Symbol i2c multiply defined (by main.cpp.K64F.o and wire.cpp.K64F.o).

Is there a possible solution or workaround?

1 Answer

9 years, 6 months ago.

Hello Tobias,

It seems to me the compiler complains about the symbol i2c. A public symbol can only be defined once. The code you show doesn't display the problem to me, but if you include main.h in main.cpp, and include wire.h in main.cpp, the compiler will complain. Moving the definition from the header file to the source file and declaring it extern in the header file should solve your problem.

Regards, Andreas

Accepted Answer