Initializing an extern array

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Initializing an extern array

Sylvester Steele

Hi,
I'd like to know how to initialize and use an extern int array. I've been
stuck for sometime now.

Sylvester
--
View this message in context: http://www.nabble.com/Initializing-an-extern-array-tp20552435p20552435.html
Sent from the MinGW - User mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Initializing an extern array

Tor Lillqvist
> I'd like to know how to initialize and use an extern int array. I've been
> stuck for sometime now.

At least tell us then what you have tried and what errors you have encountered.

Is the array in a library that you build as a DLL? Then you need to
use the __declspec(dllexport) attribute when defining the array and
__declspec(dllimport) when declaring it for use from code outside the
DLL. Google for examples. This is a pain and leads to a mess of ifdefs
especially if you want to make it possible to build the library either
as a DLL or as a static library. It would be much easier to just have
a function instead that returns a pointer to the array.

--tml

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Initializing an extern array

Sylvester Steele

At least tell us then what you have tried and what errors you have
encountered.

Sorry about that.

I am basically declaring an extern array in a file say global1.h. The I am
using it in 2-3 other files. I include this file.

I declare it like this:

extern int outputs[4];

to use this extern array I do:

int outputs[4]; - this outside of any function, in file that I want to
access the extern variable.

If I do this in 1 file only (ie declaration an 1 file and use in 1 more
different file) the program compiles.

However if I use it more than one file (declaration an 1 file and use in 2
or more different files) it does not compile. The error I get is:

int outputs[4] previously declared here - in 1 file
and redefinition of int outputs[4] - in another.

Please help!
Thanks,
Sylvester


Tor Lillqvist wrote:

>
>> I'd like to know how to initialize and use an extern int array. I've been
>> stuck for sometime now.
>
> At least tell us then what you have tried and what errors you have
> encountered.
>
> Is the array in a library that you build as a DLL? Then you need to
> use the __declspec(dllexport) attribute when defining the array and
> __declspec(dllimport) when declaring it for use from code outside the
> DLL. Google for examples. This is a pain and leads to a mess of ifdefs
> especially if you want to make it possible to build the library either
> as a DLL or as a static library. It would be much easier to just have
> a function instead that returns a pointer to the array.
>
> --tml
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's
> challenge
> Build the coolest Linux based applications with Moblin SDK & win great
> prizes
> Grand prize is a trip for two to an Open Source event anywhere in the
> world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
>
>

--
View this message in context: http://www.nabble.com/Initializing-an-extern-array-tp20552435p20614307.html
Sent from the MinGW - User mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists. We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Initializing an extern array

Greg Chicares
[Please don't top-post:
  http://en.wikipedia.org/wiki/Posting_style#Top-posting
Thanks.]

On 2008-11-21 02:40Z, Sylvester Steele wrote:
>
> I am basically declaring an extern array in a file say global1.h. The I am
> using it in 2-3 other files. I include this file.
>
> I declare it like this:
>
> extern int outputs[4];

This declaration enables you to access and use the variable,
as long as it's defined elsewhere. I'd generally omit the
dimension '4' here: then you don't have to change it in the
header when you change it in the definition.

> to use this extern array I do:
>
> int outputs[4]; - this outside of any function, in file that I want to
> access the extern variable.

That's a definition. You need exactly one definition, in
exactly one file. The 'extern' declaration enables the
variable defined here to be accessed elsewhere.

> If I do this in 1 file only (ie declaration an 1 file and use in 1 more
> different file) the program compiles.
>
> However if I use it more than one file (declaration an 1 file and use in 2
> or more different files) it does not compile.

One declaration is sufficient for you to use the variable in
any file that includes the header containing the declaration.
You don't have to repeat the definition: in fact, it's an error.
A definition allocates space. You want to allocate space only
once for a variable.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists. We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Initializing an extern array

Sylvester Steele
Greg Chicares wrote:
[Please don't top-post:
  http://en.wikipedia.org/wiki/Posting_style#Top-posting
Thanks.]

On 2008-11-21 02:40Z, Sylvester Steele wrote:
  
I am basically declaring an extern array in a file say global1.h. The I am
using it in 2-3 other files. I include this file. 

I declare it like this:

extern int outputs[4];
    

This declaration enables you to access and use the variable,
as long as it's defined elsewhere. I'd generally omit the
dimension '4' here: then you don't have to change it in the
header when you change it in the definition.

  
to use this extern array I do:

int outputs[4]; - this outside of any function, in file that I want to
access the extern variable.
    

That's a definition. You need exactly one definition, in
exactly one file. The 'extern' declaration enables the
variable defined here to be accessed elsewhere.

  
If I do this in 1 file only (ie declaration an 1 file and use in 1 more
different file) the program compiles. 

However if I use it more than one file (declaration an 1 file and use in 2
or more different files) it does not compile.
    

One declaration is sufficient for you to use the variable in
any file that includes the header containing the declaration.
You don't have to repeat the definition: in fact, it's an error.
A definition allocates space. You want to allocate space only
once for a variable.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists. We ask that you be polite and do the same.

  
Thanks a lot. I'll try this.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists. We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Initializing an extern array

Sylvester Steele
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
_______________________________________________

Thanks a lot! It worked!

On Fri, Nov 21, 2008 at 9:50 AM, Sylvester <[hidden email]> wrote:
Greg Chicares wrote:
[Please don't top-post:
  http://en.wikipedia.org/wiki/Posting_style#Top-posting
Thanks.]

On 2008-11-21 02:40Z, Sylvester Steele wrote:
  
I am basically declaring an extern array in a file say global1.h. The I am
using it in 2-3 other files. I include this file. 

I declare it like this:

extern int outputs[4];
    
This declaration enables you to access and use the variable,
as long as it's defined elsewhere. I'd generally omit the
dimension '4' here: then you don't have to change it in the
header when you change it in the definition.

  
to use this extern array I do:

int outputs[4]; - this outside of any function, in file that I want to
access the extern variable.
    
That's a definition. You need exactly one definition, in
exactly one file. The 'extern' declaration enables the
variable defined here to be accessed elsewhere.

  
If I do this in 1 file only (ie declaration an 1 file and use in 1 more
different file) the program compiles. 

However if I use it more than one file (declaration an 1 file and use in 2
or more different files) it does not compile.
    
One declaration is sufficient for you to use the variable in
any file that includes the header containing the declaration.
You don't have to repeat the definition: in fact, it's an error.
A definition allocates space. You want to allocate space only
once for a variable.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists. We ask that you be polite and do the same.

  
Thanks a lot. I'll try this.


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users