How to pass a sketch instance to a library ?

Hi,

I would like to use a instance defined in a sketch in a library.
Attached the sketch and library

The following is the simple test that is not working

  1. In a sketch I (Test.ino):
    #include <Testlib.h>

and the definition of:
EthernetClient client;

In the library (Testlib.cpp) I use a function such as:
void clientWrite ()
{
client.write(“Hello”);
}

Compilation gives error such:
…/Testlib/Testlib.cpp: In function ‘void clientWrite()’:
…/Testlib/Testlib.cpp:5:3: error: ‘client’ was not declared in this scope
client.write(“Hello”);
^
Error compiling.

  1. If I define the client in the library Testlib.h and not in the Test.ino sketch
    #ifndef Testlib_H
    #define Testlib_H
    #include <Arduino.h>
    #include <Ethernet.h>
    EthernetClient client;
    void clientWrite();
    #endif

Compilation gives error such:
/var/folders/bq/1_7b3x851wj3d2wxhqqntcnr0000gp/T/build5671766976262198974.tmp/Testlib/Testlib.cpp.o:(.bss.client+0x0): multiple definition of `client’
/var/folders/bq/1_7b3x851wj3d2wxhqqntcnr0000gp/T/build5671766976262198974.tmp/Test.cpp.o:/Applications/Test.ino:21: first defined here
collect2: error: ld returned 1 exit status
Error compiling.

  1. If I define the client in the library Testlib.cpp and not in the Test.ino sketch
    #include <Testlib.h>
    EthernetClient client;
    void clientWrite ()
    {
    client.write(“Hello”);
    }
    Compilation gives error such:
    /var/folders/bq/1_7b3x851wj3d2wxhqqntcnr0000gp/T/build5671766976262198974.tmp/Test.cpp.o
    Test.ino: In function ‘void loop()’:
    Test.ino:22:2: error: ‘client’ was not declared in this scope
    Error compiling.

  2. if I defined in the sketch and in the lib ray (.cpp or .h)

/var/folders/bq/1_7b3x851wj3d2wxhqqntcnr0000gp/T/build5671766976262198974.tmp/Testlib/Testlib.cpp.o:(.bss.client+0x0): multiple definition of `client’
/var/folders/bq/1_7b3x851wj3d2wxhqqntcnr0000gp/T/build5671766976262198974.tmp/Test.cpp.o:/Applications/Test.ino:21: first defined here
collect2: error: ld returned 1 exit status
Error compiling.

or

/var/folders/bq/1_7b3x851wj3d2wxhqqntcnr0000gp/T/build5671766976262198974.tmp/Test.cpp.o
Test.ino:13:16: error: redefinition of ‘EthernetClient client’
In file included from Test.ino:2:0:
…/Testlib/Testlib.h:5:16: error: ‘EthernetClient client’ previously declared here
EthernetClient client;
^
Error compiling.

What am I doing wrong and/or is there a solution ??

Thanks in advance
Robert

Testlib.h (110 Bytes)

Testlib.cpp (93 Bytes)

Test.ino (523 Bytes)

Do you need to pass an actual object instance, or will a method pointer do?
There are examples of libraries, like pin change interrupt and scheduler that accept function pointers to user callbacks.

If all you need is to pass the object so you can use the stream features (read/write) then you can do something like this:

void clientWrite( Stream &client )
{
** client.write("Hello");**
}

This works as Stream is a base class for EthernetClient

If you need the complete object to work with, you can pass a reference to it also:

void clientWrite( EthernetClient &client )
{
** client.write("Hello");**
}

Both these examples allow you to 'pass' the 'client' defined in your sketch, rather than redefining it in the library like you had.
clientWrite( client ); //in sketch

You will have to include Arduino.h & Ethernet.h into your library too.

Formidable !!!

Thanks pYro_65 for your illuminate answer

Robert