0
Offline
Jr. Member
Karma: 0
Posts: 95
Arduino rocks
|
 |
« on: January 23, 2013, 10:25:32 am » |
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. Any help would be greatly appreciated. Thanks, Loren
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35575
Seattle, WA USA
|
 |
« Reply #1 on: January 23, 2013, 10:43:02 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 95
Arduino rocks
|
 |
« Reply #2 on: January 23, 2013, 09:12:21 pm » |
What is mentioned above makes sense here is what I attempted: #ifndef Eth_h #define Eth_h
#include <SPI.h> #include <Ethernet.h>
byte mac [5]; IPAddress ip;
class Eth{ public: void init();
}; #endif Which returns the error: error: 'IPAddress' does not name a type I thought that IPAddress was a member of the Ethernet source. What am I not doing or missing?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 836
|
 |
« Reply #3 on: January 23, 2013, 11:21:38 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35575
Seattle, WA USA
|
 |
« Reply #4 on: January 24, 2013, 03:12:37 am » |
here is what I attempted: What does your sketch look like? Are you including Ethernet.h in the sketch?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #5 on: January 24, 2013, 04:18:46 am » |
IMHO you should not declare variables in .h files.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35575
Seattle, WA USA
|
 |
« Reply #6 on: January 24, 2013, 05:54:54 am » |
IMHO you should not declare variables in .h files. Why not? It is standard practice, recommended everywhere else. What about class definitions?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 95
Arduino rocks
|
 |
« Reply #7 on: January 24, 2013, 06:52:17 am » |
What does your sketch look like? Are you including Ethernet.h in the sketch?
No.... And that was the problem! Thanks
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #8 on: January 24, 2013, 07:50:35 am » |
IMHO you should not declare variables in .h files. Why not? It is standard practice, recommended everywhere else. What about class definitions? Uh ? Are you saying class definitions and variable declarations are the same thing ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #9 on: January 24, 2013, 08:05:46 am » |
As for declaring variables in .h files being standard practice, let's look at EEPROM.h/.cpp: #ifndef EEPROM_h #define EEPROM_h
#include <inttypes.h>
class EEPROMClass { public: uint8_t read(int); void write(int, uint8_t); };
extern EEPROMClass EEPROM; // <<< reference to a variable declared elsewhere
#endif
#include <avr/eeprom.h> #include "Arduino.h" #include "EEPROM.h"
uint8_t EEPROMClass::read(int address) { return eeprom_read_byte((unsigned char *) address); }
void EEPROMClass::write(int address, uint8_t value) { eeprom_write_byte((unsigned char *) address, value); }
EEPROMClass EEPROM; // <<< variable declaration is in .cpp file
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35575
Seattle, WA USA
|
 |
« Reply #10 on: January 24, 2013, 08:57:20 am » |
Uh ? Are you saying class definitions and variable declarations are the same thing ? No, I never said that. I'm asking, if you don't like declaring variables in the header file, how you feel about defining classes in header files. I'm trying to determine whether to take you seriously.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #11 on: January 24, 2013, 09:07:33 am » |
Uh ? Are you saying class definitions and variable declarations are the same thing ? No, I never said that. I'm asking, if you don't like declaring variables in the header file, how you feel about defining classes in header files. I'm sorry, that's nonsense. Declaration != definition. Classes are defined in header files. Variables should be declared in .cpp files. You didn't comment on the EEPROM example. I'm trying to determine whether to take you seriously.
I will ignore personal attacks. (edit: typo)
|
|
|
|
« Last Edit: January 24, 2013, 10:14:24 am by tuxduino »
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 836
|
 |
« Reply #12 on: January 24, 2013, 09:09:20 am » |
What does your sketch look like? Are you including Ethernet.h in the sketch?
No.... And that was the problem! Thanks Eh??? It looks like it is there in your code, in your second post on this thread.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35575
Seattle, WA USA
|
 |
« Reply #13 on: January 24, 2013, 09:30:01 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35575
Seattle, WA USA
|
 |
« Reply #14 on: January 24, 2013, 09:30:58 am » |
I will ignore personal attacks. It was not a personal attack. I'm just trying to understand your position. It is not common practice.
|
|
|
|
|
Logged
|
|
|
|
|
|