[Solved] Moving a variable from .cpp to the main program?

I’m using the ethercard.h library (https://github.com/jcw/ethercard) - in the library there is a file called dhcp.cpp with:

static char hostname[] = "Arduino-00";

I would like to move this variable to my main program so that it makes it easier changing the value with each project where I use the ethercard library.

I tried uncommenting the line in the ethercard.cpp and adding it to my main sketch however it does not work (I thought it might not be that simple)

\dhcp.cpp:184: error: ‘hostname’ was not declared in this scope

Any ideas? I’ve also tried using this in my main sketch

extern char hostname[] = "Arduino-00";

But no go. I think I"m on the right track (hopefully), but not quite there.

I think I've got it. In the ethernet.h put:

extern char hostname[];

Seems to compile and work ok.

You can't initialise a variable in an extern statement. All the extern does is tell the compiler that the variable exists somewhere else.

If you want the variable to be visible outside of the file it is defined in then you should not declare it as static.

To share a variable between files, you need to define it in one .cpp/.ino file:

char hostname[] = "Arduino-00";

In any other files that need to access it, you need to include an external declaration:

extern char hostname[];

The declaration is conventionally put in a .h file which is #included in whichever source files need access to this variable.

It doesn't matter to the compiler which file you define the variable in, but usually the variable will be tightly coupled to one module which you could think of as owning it, and it makes sense to define it in that module.

Thanks - got a much better understanding of it now.