I'm working on a project that I think will be fairly large. I'm hoping to write some libraries to help maintain the ability to read through it easily. I did go through the library tutorial but I am confused about where to put a some of the declarations that the library will need to access.
for example I'm working with a ethernet shield and need to declare this all of this:
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
I got an error when I tried to declare it at the top of the header file and when I tried to put it at the top of the source file. I did try and declare all of it in the main file but that also created problems. I assume that in this scenario the header and source file don't have access to the variables.
I got an error when I tried to declare it at the top of the header file
Those statements declare and initialize the variables. You need to declare them in the header file and initialize them in the source file if you want to share them.
Ethernet.h appears to want to include IPAddress.h , so I guess the IPAddress
class is in that .h file. I can't find it on my computer, but I never used Ethernet with
arduino, so I dunno.
Try searching for IPAddress.h on your computer and see if you have it and it is in the
correct place.
It looks like it is there in your code, in your second post on this thread.
The Ethernet.h files was included in the library, as it needs to be. But, it was not included in the sketch.
The Arduino IDE copies files to another directory for compiling. What it copies are the header files included in the sketch plus the sketch plus any source files that go with the header files, plus any other files in the sketch directory.
What it does not copy are header files referenced in other files in the sketch directory. So, since the sketch didn't include Ethernet.h, that file was not copied to the build directory, so it was not available for the library to include/use, so the symbols defined in it were not available. IPAddress is one of those symbols.
eth.cpp.o:eth.cpp:14: multiple definition of `ip'
consRemoteTwo.cpp.o:consRemoteTwo.cpp:11: first defined here
/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld: Disabling relaxation: it will not work with multiple definitions
eth.cpp.o:eth.cpp:14: multiple definition of `server'
consRemoteTwo.cpp.o:consRemoteTwo.cpp:11: first defined here
eth.cpp.o:(.data.mac+0x0): multiple definition of `mac'
consRemoteTwo.cpp.o:(.data.mac+0x0): first defined here
Well, you need to be careful not to define things twice.
That's why most header files have that #ifndef stuff at the beginning, to prevent them being included twice.
Your "variables" mac and server are being defined twice. This could mean one of two things:
(a) the same variable is being included twice, or
(b) two different variables are being defined, using the same name.
To distinguish these cases, try re-naming your instances of these variables to something else. Like mac_xxx and server_xxx.
And then see what happens.
michinyon:
Well, you need to be careful not to define things twice.
That's why most header files have that #ifndef stuff at the beginning, to prevent them being included twice.
Your "variables" mac and server are being defined twice. This could mean one of two things:
(a) the same variable is being included twice, or
(b) two different variables are being defined, using the same name.
To distinguish these cases, try re-naming your instances of these variables to something else. Like mac_xxx and server_xxx.
And then see what happens.
I tried to rename the variables. The same errors are still occurring.
I did lose the () to create the class. No changes in the errors there either.
Please explain why those are global variables (that is why they are created twice), rather than class fields.
You could define the variables in the sketch, and add extern in front of the declaration statements in the header. That way, the header file would simply affirm that the variables are defined somewhere else, and would allow access to them, wherever they are defined.