Libraries referencing other libraries?

Hello all,

I'm very new to the Arduino scene, but very old to the programming scene. And I'm having serious difficulty with getting libraries to use other libraries. I wanted to write a simple serial LCD library to wrap the various commands and what not involved with communicating with my serial backpack enabled lcd. First off, I had to invert the SoftwareSerial library, as I needed TTL ( inverted ) serial to communicate with my display. This library works like a champ when used directly from my sketches.

Then I decided to write another library that could wrap up the details of lcd communication. This library should use the inverted serial library I wrote, but the arduino ide gets seriously upset when you try to do that. I had to play games with the includes, the class members, etc. Long story short, it doesn't work.

After messing with this for a few hours, I started to wonder if it wasn't something in the ide, so I grabbed the SSerial2Mobile library referenced on the arduino website. This library uses SoftwareSerial to send sms messages through your cell phone. Well, if you install this library, open a new sketch, click to "import" this library to put in all the includes, then compile it, you get massive errors.

I've read lots of posts from people saying they're happily using this library, so I'm wondering what the deal is. Am I doing something wrong? Are other people experiencing this same issue? I've tested this with Arduino 0017 on mac and windows with the same problems. Can someone confirm/deny? I really appreciate any help.

Thanks

Another example: webduino. I downloaded webduino 1.4, dropped the webduino folder in Arduino/libraries, restarted Arduino 0017, started a new sketch, included WebServer.h, put in dummy setup and loop functions, and when I click verify, here's what I get:

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:96: error: expected class-name before '{' token

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:141: error: expected ',' or '...' before '*' token

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:141: error: ISO C++ forbids declaration of 'prog_uchar' with no type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:144: error: expected ',' or '...' before '*' token

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:144: error: ISO C++ forbids declaration of 'prog_uchar' with no type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:199: error: 'uint8_t' has not been declared

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:201: error: expected ',' or '...' before '*' token

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:201: error: ISO C++ forbids declaration of 'uint8_t' with no type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:201: error: 'virtual void WebServer::write(int)' cannot be overloaded

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:199: error: with 'virtual void WebServer::write(int)'

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:205: error: 'Server' does not name a type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:206: error: 'Client' does not name a type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: In constructor 'WebServer::WebServer(const char*, int)':

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:243: error: class 'WebServer' does not have any field named 'm_server'

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:244: error: class 'WebServer' does not have any field named 'm_client'

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: In member function 'void WebServer::begin()':

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:256: error: 'm_server' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: At global scope:

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:278: error: variable or field 'write' declared void

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:278: error: 'uint8_t' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: In member function 'virtual void WebServer::write(const char*)':

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:285: error: 'm_client' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: At global scope:

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:288: error: expected ',' or '...' before '*' token

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:288: error: ISO C++ forbids declaration of 'uint8_t' with no type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: In member function 'virtual void WebServer::write(int)':

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:290: error: 'm_client' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:290: error: 'buffer' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:290: error: 'size' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: In member function 'void WebServer::write(const char*, size_t)':

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:295: error: 'm_client' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:295: error: ISO C++ forbids declaration of 'type name' with no type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:295: error: ISO C++ forbids declaration of 'type name' with no type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:295: error: expected primary-expression before 'const'

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:295: error: expected `)' before 'const'

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: At global scope:

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:298: error: expected ',' or '...' before '*' token

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:298: error: ISO C++ forbids declaration of 'prog_uchar' with no type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: In member function 'void WebServer::writeP(int)':

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:302: error: 'uint8_t' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:302: error: expected `;' before 'buffer'

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:305: error: 'length' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:309: error: 'm_client' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:309: error: 'buffer' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:313: error: 'buffer' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:313: error: 'data' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:313: error: 'pgm_read_byte' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:317: error: 'm_client' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:317: error: 'buffer' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: At global scope:

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:320: error: expected ',' or '...' before '*' token

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:320: error: ISO C++ forbids declaration of 'prog_uchar' with no type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: In member function 'void WebServer::printP(int)':

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:324: error: 'uint8_t' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:324: error: expected `;' before 'buffer'

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:327: error: 'buffer' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:327: error: 'str' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:327: error: 'pgm_read_byte' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:331: error: 'm_client' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:338: error: 'm_client' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:338: error: 'buffer' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: In member function 'void WebServer::printCRLF()':

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:343: error: 'm_client' was not declared in this scope

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:343: error: ISO C++ forbids declaration of 'type name' with no type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:343: error: ISO C++ forbids declaration of 'type name' with no type

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:343: error: expected primary-expression before 'const'

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:343: error: expected `)' before 'const'

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h: In member function 'void WebServer::processConnection(char*, int*)':

/Users/nisburgh/Documents/Arduino/libraries/webduino/WebServer.h:397: error: 'm_client' was not declared in this scope

That's not even all of it. It goes on like this for a while.. Any ideas?

Update: Adding Ethernet.h to the sketch allows it to compile correctly. So now I'm wondering what WebServer.h does that allows it to play nicely with other libraries.. Going to look in to this. If you already know what's going on, please holler at me. :) I'd greatly appreciate it!

Thanks in advance - I hope it's me doing something dumb! ;)

Hi, sorry I can only confirm your troubles: You can't directly access a library from another library. One of the things where the Arduino-build-process fails.

The reason is the way the compiler is called with the list of include-dirs. If libA uses functions from libB, libA can only see the libB.h header (during the compiler run) when both libraries are imported into the sketch.

As with your WebDuino-lib: it works when you add Ethernet.h to your sketch because then the classes and methods from the Ethernet-lib are found, but it doesn't work as standalone library which includes Ethernet.h internally.

Eberhard

Aye, I'm seeing this bug. I've reworked a few things and now have it working by including the dependencies in the sketch rather than the library.

Any idea when this might be fixed?

Thanks!

I wish I found this thread sooner. Thanks for posting the fix to this problem.

I hear ya! No problem - I've written several libraries since and no longer have problems, now that I know I have to include things at the sketch level. I never include anything in the library any more. Hope this helps!

Cheers

I have possibly a related problem. I have created a library that uses NewSoftSerial object “mySerial” as a static in the MyLib.cpp code.
MyLib.cpp includes “NewSoftSerial.h” and so does the sketch. It compiles but when it runs, the constructor for the class in the MyLib.cpp code.
The class object from MyLib is a global in my sketch (outside of setup() and loop()). This recursive behavior does not occur when I don’t use NewSoftSerial in my own library. Has anyone seen this runaway recursion before???

sketch

#include <NewSoftSerial.h>

KeypadBV4505 keypad(1);
#include <KeypadBV4505.h>

end of sketch

####### MyLib.cpp #########
#include “WProgram.h”
#include “KeypadBV4505.h”

#include <NewSoftSerial.h>

// Statics
#define SOFT_RX 2
#define SOFT_TX 3
NewSoftSerial mySerial(SOFT_RX, SOFT_TX);
#define SERIAL_BV mySerial
//#define SERIAL_BV Serial2

KeypadBV4505::KeypadBV4505(int serialPort)
{
_serialPort = serialPort; // TODO: Serial port selection not implemented yet
_pinTimeoutMillis = PIN_ENTRY_TIMEOUT_MILLIS;
_pinDigits = NUM_PIN_DIGITS_DEFAULT;
_debug = false;
_piezoSpeakerPin = 0; // An Arduino pin of zero disables bells and whistles (piezo tone feedback)

if (_debug) Serial.println(“Calling init - BV4505 TTL Serial 12-key Keypad Interface”);
// initialize both serial ports:
Serial.begin(19200); // This baud and below don’t have to be the same.
Serial.println(“FOO”);
SERIAL_BV.begin(9600); //

initKeypad();
if (_debug) Serial.println(“BV4505 TTL Serial 12-key Keypad Interface Initialized”);
}

################################

It compiles but when it runs, the constructor for the class in the MyLib.cpp code.

The constructor does (or doesn't do) what?

The print "FOO" spews down the serial console at a very high rate. Because of that I assumed that the constructor is getting called recursively for some reason. Does that make any sense? I may not be describing things with the correct terms.

It appears as though the constructor is called repeatedly, not recursively. However, we can't see enough of your code to determine what the problem is.

Instead of having the NewSoftSerial object as a static in my cpp file I just instantiated it on demand inside the member funcs for now and the problem went away. After I clean up the files I can post them. I was never a wiz bang C++ guy. I went from years of C to Python, Java, etc so it taking me a little bit to get up to speed with C++ semantics, scoping, etc again. :frowning: