Error "undefined reference to"

Hi all,

I try to create Arduino library for UDOO but I've an issue during compilation. Indeed, I suceed to compile my program by selecting classic Arduino board like "Arduino MEGA" but when I select "UDOO Neo (Cortex M4)" in Arduino IDE, I've the following error :

C:\Users\Ivan\AppData\Local\Temp\arduino_build_499711\sketch\Morse.ino.cpp.o: In function `loop':

C:\Users\Ivan\Documents\Arduino\Morse/Morse.ino:15: undefined reference to `gougou'

collect2.exe: error: ld returned 1 exit status

exit status 1

I did some testing with very simple program splited into 3 files and I've the same error:

.ino file

#include "Morse.h"



void setup()
{
}

void loop()
{

  gougou.test();
  delay(3000);
}

.cpp file :

#include "Arduino.h"
#include "Morse.h"



void Morse::test()
{

  
}

.h file

#ifndef Morse_h
#define Morse_h

#include "Arduino.h"

class Morse
{
  public:
    Morse(int pin);
    void dot();
    void dash();
    void test();
  private:
    int _pin;
};
extern Morse gougou;

#endif

I have the felling that the origin of error comes from "extern Morse gougou;" but I don't understand why I can compile by selecting classic Arduino board.

Thank for your help

You mangled the posting of your .h and .cpp files, so it is difficult to make sense of them. However, it appears that, at the end of one of them, you declare that there is, somewhere external to the header file, an instance of the class called gougou. You, you never create an instance of the class with that name.

You should not insist that the user use that name, and only that one instance of your class.

I'm sorry for the shaping of the code.

Thank, for your reply. Indeed, I fixed the problem by adding "Morse gougou" into the cpp file :

#include "Arduino.h"
#include "Morse.h"


Morse gougou;

void Morse::test()
{

  
}

However, I still don't understand why I could compile my program by selecting a classic Arduino bord without create an instance of the class with "gougou" name and why I've an issue by selecting UDOO bord.

But why do you make an instance in your library? Do you only want once instance? This does not limit you to that.

The normal route would be to make an instance in the program you use the code in. That way you can have multiple instances of your class.

Septillion,

The code above is just an exemple to illustrate my problem. The real library that I program is a communication protocol and uses the serial.I want to prevent the use of several instance. That is why I want only one instance.

this is, in NO way, preventing more then one instance ;)

And what if someone wants to use this library with Serial2?

You prevent creating more than one instance of the class by making the constructor private, and providing a public method that returns the one existing instance, or creates an instance and returns a pointer to the new instance.

Look at how HardwareSerial manages to allow only one instance of the class per set of pins.