2

I have these files:

  • Project.ino
  • Sensor.h
  • Sensor.cpp
  • IMU/L3G.h
  • IMU/L3G.cpp

Sensor.h

#ifndef Sensor_h
#define Sensor_h

#include <EEPROM.h>
#include "IMU/L3G.h"

#define CUTOFF_FREQ 30
#define FREQ 400


class Sensor {
public:
    Sensor();
    ~Sensor();    
    void Init(void);

pivate:
    L3G gyro;
};
#endif

Sensor.cpp

#include "Sensor.h"

Sensor::Sensor()
{

}
Sensor::~Sensor()
{

}

void Sensor::Init(void)
{
  if (!gyro.init())
  {
    Serial.println("Failed to autodetect gyro type!");
    while (1);
  }
  gyro.enableDefault();
}

Project.ino

#include <i2c_t3.h>
#include <EEPROM.h>
#include <Servo.h>
#include "Sensor.h"


Sensor sensor = Sensor(); 


void setup() {

  sensor.Init(); 
}

I got this error

/var/folders/8q/rbt7zf3d28b3k9t6d24k5sn00000gn/T/arduino_build_748953/sketch/Sensor.cpp.o: In function `Sensor::Init()':
/var/folders/8q/rbt7zf3d28b3k9t6d24k5sn00000gn/T/arduino_build_748953/sketch/Sensor.cpp:15: undefined reference to `L3G::init(unsigned char, unsigned char)'
/var/folders/8q/rbt7zf3d28b3k9t6d24k5sn00000gn/T/arduino_build_748953/sketch/Sensor.cpp:20: undefined reference to `L3G::enableDefault()'

So, what is wrong?

4
  • 1
    pivate? typo?
    – Juraj
    Commented Jul 18, 2018 at 4:54
  • Your question is tagged arduino-uno and yet the i2c_t3 is for teensy devices Commented Jul 18, 2018 at 10:50
  • @Juraj - once the typo has been fixed, then the OP's error is seen. Commented Jul 18, 2018 at 15:56
  • What platform are you compiling on (OSX, Windows, Linux)? What version of the IDE are you using? 1.8.5? Where did you source your LMU library from? Commented Jul 18, 2018 at 15:57

1 Answer 1

2

As Juraj rightly points out, you have a typo, which should give you the following compile time error:

In file included from sketch/Sensor.cpp:1:0:
Sensor.h:17: error: 'pivate' does not name a type
 pivate:
 ^

Here is the correct code for Sensor.h

#ifndef Sensor_h
#define Sensor_h

#include <EEPROM.h>
#include "IMU/L3G.h"

#define CUTOFF_FREQ 30
#define FREQ 400


class Sensor {
public:
    Sensor();
    ~Sensor();    
    void Init(void);

private:
    L3G gyro;
};
#endif

Using Arduino IDE 1.8.5 with Teensyduino 1.42 with:

However, once the typo has been corrected, as above, then I too can reproduce your problem (sort of), with these link errors:

/var/folders/kp/xv3y9k114yq2_64pf8zwh76h0000gp/T/arduino_build_136680/sketch/Sensor.cpp.o: In function `Sensor::Sensor()':
/var/folders/kp/xv3y9k114yq2_64pf8zwh76h0000gp/T/arduino_build_136680/sketch/Sensor.cpp:3: undefined reference to `L3G::L3G()'
/var/folders/kp/xv3y9k114yq2_64pf8zwh76h0000gp/T/arduino_build_136680/sketch/Sensor.cpp.o: In function `Sensor::Init()':
/var/folders/kp/xv3y9k114yq2_64pf8zwh76h0000gp/T/arduino_build_136680/sketch/Sensor.cpp:14: undefined reference to `L3G::init(L3G::deviceType, L3G::sa0State)'
/var/folders/kp/xv3y9k114yq2_64pf8zwh76h0000gp/T/arduino_build_136680/sketch/Sensor.cpp:19: undefined reference to `L3G::enableDefault()'
/var/folders/kp/xv3y9k114yq2_64pf8zwh76h0000gp/T/arduino_build_136680/core/core.a(main.cpp.o): In function `main':
/Volumes/Home/Users/user/Local_Apps/Arduino/Arduino1.8.5-Teensy.app/Contents/Java/hardware/teensy/avr/cores/teensy3/main.cpp:53: undefined reference to `loop'
collect2: error: ld returned 1 exit status
Multiple libraries were found for "Servo.h"
 Used: /Volumes/Home/Users/user/Local_Apps/Arduino/Arduino1.8.5-Teensy.app/Contents/Java/hardware/teensy/avr/libraries/Servo
 Not used: /Volumes/Home/Users/user/Local_Apps/Arduino/Arduino1.8.5-Teensy.app/Contents/Java/libraries/Servo
Error compiling for board Teensy 3.5.

You are probably getting this error, because you have placed the files L3G.cpp and L3G.h in a folder called IMU in the same folder as your .ino file. This causes the undefined error, as, while the IDE sees the declaration in the L3G.h file, the IDE can not find the definition in the L3G.cpp file.

This is easy to resolve.

If you download the l3g-arduino-master.zip file from https://github.com/pololu/l3g-arduino, and from within the Arduino IDE you then add the zipped up L3G library to the Arduino library, using Sketch ► Include Library ► Add. ZIP Library... and then change the line

#include "IMU/L3G.h"

to

#include <L3G.h>

Then it should compile and link without a problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.