Undefined reference to error

I’m trying to build my project and I encountered an undefined reference issue. I believe this issue might not be caused by platformio but I hope that the community can help out :grinning:

Here’s the build error:

    C:\Users\User\AppData\Local\Temp\ccQAkhzT.ltrans0.ltrans.o: In function `main':
    <artificial>:(.text.startup+0x2b2): undefined reference to `imu'
    <artificial>:(.text.startup+0x2c0): undefined reference to `imu'
    <artificial>:(.text.startup+0x2d0): undefined reference to `imu'
    <artificial>:(.text.startup+0x2de): undefined reference to `imu'
    <artificial>:(.text.startup+0x2ee): undefined reference to `imu'
    C:\Users\User\AppData\Local\Temp\ccQAkhzT.ltrans0.ltrans.o:<artificial>:(.text.startup+0x2f6): more undefined references to `imu' follow
    collect2.exe: error: ld returned 1 exit status
    scons: *** [.pio\build\uno\firmware.elf] Error 1
     [ERROR] Took 8.49 seconds

main.cpp gist:

    #include "IMU.h"

    void setup() {
     Serial.begin(38400);
     imu.init();
    }

IMU.h:

    #ifndef GREENFLIGHT_IMU_H
    #define GREENFLIGHT_IMU_H

    #include "MPU6050_6Axis_MotionApps20.h"

    class IMU {
    public:
        void init();
        void updateYPR();
        float getPitch();
        float getYaw();
        float getRoll();

    protected:
        MPU6050 mpu;
    };

    extern IMU imu;
    #endif

Definitions of the declared method in IMU.h is written in IMU.cpp

I suspect it is due to main.cpp including IMU.h before the definition in IMU.cpp is declared/compiled.

It cannot find where you defined the imu object. Do you have the statement

IMU imu;

at the top of the IMU.cpp?

1 Like