Pages: [1] 2   Go Down
Author Topic: declarations in libraries  (Read 1559 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 155
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
// 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
Offline Offline
Brattain Member
*****
Karma: 632
Posts: 50182
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Full Member
***
Karma: 0
Posts: 155
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is mentioned above makes sense  here is what I attempted:

Code:
#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: 
Code:
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 Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 632
Posts: 50182
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
here is what I attempted:
What does your sketch look like? Are you including Ethernet.h in the sketch?
Logged

Offline 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)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

IMHO you should not declare variables in .h files.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 632
Posts: 50182
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Full Member
***
Karma: 0
Posts: 155
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


What does your sketch look like? Are you including Ethernet.h in the sketch?

No....  And that was the problem!  Thanks
Logged

Offline 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)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 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)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As for declaring variables in .h files being standard practice, let's look at EEPROM.h/.cpp:

Code:
#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


Code:
#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
Offline Offline
Brattain Member
*****
Karma: 632
Posts: 50182
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 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)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Offline Offline
Brattain Member
*****
Karma: 632
Posts: 50182
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Offline Offline
Brattain Member
*****
Karma: 632
Posts: 50182
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

Pages: [1] 2   Go Up
Jump to: