Issue with a very simple library

Dear All,

I am a beginer and I am trying to create my first library.

My terminal display that error message:

sim908_v6.cpp.o: In function __static_initialization_and_destruction_0': /Applications/sim908_v6.ino:3: undefined reference to Sim908::Sim908()'
sim908_v6.cpp.o: In function loop': /Applications/sim908_v6.ino:16: undefined reference to Sim908::blinkLed(int, int, int)'

May I ask you to to help me about that error message?

Here is my code.

First I create two files in
/libraries/Sim908/Sim908.h

/*
  Sim908.h - Library 
*/

#ifndef Sim908_h
#define Sim908_h

#include "Arduino.h"


class Sim908{
	public:
		Sim908();
		void blinkLed(int lPin, int nBlink, int msec);
	
	private:
};

#endif

/libraries/Sim908/Sim908.cpp

#include "Arduino.h"
#include "Sim908.h"

Sim908::Sim908(){

}

void Sim908::blinkLed(int lPin, int nBlink, int msec) {
   if (nBlink) {
     for (int i = 0; i < nBlink; i++) {
       digitalWrite(lPin, HIGH);
       delay(msec);
       digitalWrite(lPin, LOW);
       delay(msec);
     }
   }
 }

and
Sim908_v6.ino.

#include <Sim908.h>

Sim908 sim908;

int green = 12;


void setup()
{
  pinMode(green, OUTPUT);
}


void loop()
{
  sim908.blinkLed(green,1,1000);
  delay(3000);
}

Where is my mistake?

Many thank

Your code compiles fine ( 1.5.6 & 1.5.7 ).

Sure you posted the right file?
The error is for sim908_v6.ino and you posted sim908.ino?

Hello,
Did you try the code,
I rename the file to Sim908 (Ido not think it cause the error) and the probleme is the same.

When I compile, I still get that error message:

sim908.cpp.o: In function __static_initialization_and_destruction_0': /Applications/sim908.ino:3: undefined reference to Sim908::Sim908()'
sim908.cpp.o: In function loop': /Applications/sim908.ino:28: undefined reference to Sim908::blinkLed(int, int, int)'

(as when thr file were named Sim908_v6.ino)

pierrot10:
Did you try the code.

Yes!

C:\Users\Chris\AppData\Local\Temp\build5652238097145858837.tmp/sim908.cpp.hex

Sketch uses 1,260 bytes (3%) of program storage space. Maximum is 32,256 bytes.
Global variables use 12 bytes (0%) of dynamic memory, leaving 2,036 bytes for local variables. Maximum is 2,048 bytes.

I'll take a guess; your libraries are in the wrong spot, or if you use the name sim908_v6 for your .h file, you must name the folder the same.

You obviously do have two different codes regardless of the file name:

/Applications/sim908_v6.ino:16: undefined reference to Sim908::blinkLed(int, int, int)' /Applications/sim908.ino:<mark>28</mark>: undefined reference to Sim908::blinkLed(int, int, int)'

Just a simple correction

my library is called Sim908.
It is in folder
/libraries/Sim908/
and the two files are name Sim908.h and Sim908.cpp. Those two files are in folder /libraries/Sim908/

I created a file Sim908_v6.ino and I imported the Sim908 library. I do not think the ino file has to have the same name than the library you want to import otherwise you will not be able to import other libraries...

No, the sketch name does not have to match.

But your code does work as posted. Have you restarted the IDE since creating the library folder: http://arduino.land/FAQ/content/5/19/en/the-library-is-in-the-right-folder-but-it-does-not-work.html

I have attached your code as a single file so you can prove it works, there is a problem elsewhere.

sim908.ino (501 Bytes)

my library is called Sim908.
It is in folder
/libraries/Sim908/

Which folder is the libraries folder in ?

I am working with a Mac.
All of the library I am using are in

/Documents/Arduino/libraries/

The simple library I am trying to develop is
/Documents/Arduino/libraries/Sim908

The Sim908_v6.ino is in folder (same level than /libraries/
/Documents/Arduino/

@pyro_65

Thank for your advise.

I close Arduino IDE, restart the Macbook and reopen my ino file.
The problem is still the same.
I commented the function //sim908.blinkLed(green,1,1000); (because is another error)

and I compiled and got that error:

sim908_v6.cpp.o: In function __static_initialization_and_destruction_0': /Applications/sim908.ino:3: undefined reference to Sim908::Sim908()'

I red your link, but is it the same problem of me? I do not get an error message stating: "...was not declared in this scope".

Iam using the Arduino IDE.

I think my error come from here

previiuos:
Sim908 sim908;

now:
Sim908 sim908();

I added the ().

But why the () make the difference?
It because I add an empty cpnstructor?

Sim908::Sim908(){
}

I have a case where I do not have (), for exemple
// GPS
GPSGSM gps;

I have abother problem with my function
In my loop() I added this

void loop()
{
  sim908.blinkLed(green,1,1000);
  delay(3000);
}

That function is deéared in the Sim908.cpp

void Sim908::blinkLed(int lPin, int nBlink, int msec) {
   if (nBlink) {
     for (int i = 0; i < nBlink; i++) {
       digitalWrite(lPin, HIGH);
       delay(msec);
       digitalWrite(lPin, LOW);
       delay(msec);
     }
   }
 }

and in Sim908.h file

class Sim908{
	public:
		Sim908();
		void blinkLed(int lPin, int nBlink, int msec);
	
	private:
};

When I compile my code, the terminal display that error:
sim908_v6.ino: In function 'void loop()':
sim908_v6:30: error: cannot call member function 'void Sim908::blinkLed(int, int, int)' without object

Is a problem of type? I tried several solution but without success. may I ask you an help?
Many thank

Is a problem of type? I tried several solution but without success. may I ask you an help?

This is wrong:

Sim908 sim908();

This is defining the prototype for a function, called sim908, that takes no arguments, and returns something of type Sim908.

It is NOT creating an instance of the class.

There is NO reason for the instance name to be anything like the class name. You could, and should, create an instance like:

Sim908 blinkyThing;