Go Down

Topic: declarations in libraries (Read 1 time) previous topic - next topic

Loren

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: [Select]
// 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

PaulS

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.

Loren

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

Code: [Select]
#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: [Select]
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?

michinyon

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.

PaulS

Quote
here is what I attempted:

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

tuxduino

IMHO you should not declare variables in .h files.

PaulS

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

Why not? It is standard practice, recommended everywhere else.

What about class definitions?

Loren



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


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

tuxduino


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 ?

tuxduino

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

Code: [Select]
#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: [Select]

#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

PaulS

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.

tuxduino

#11
Jan 24, 2013, 03:07 pm Last Edit: Jan 24, 2013, 04:14 pm by tuxduino Reason: 1

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)

michinyon

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.

PaulS

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.

PaulS

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.

Go Up