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
- 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.
- 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.
-
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. -
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)