Click to See Complete Forum and Search --> : How to create global variables available functions in separate files??


delic
October 11th, 2002, 08:29 PM
I want to create a header file with a few arrays defined inside it.

Then I want all the C files that I compile to have access to those variables. how the **** do I go about this??

If i include the header in main the other files cant see it. If I include the header in all the files I get xxxxxx already defined in xxxx.obj errors..

It seems so simple yet I can not figure this out!!

Please help

oohh, I got bleeped it wont let me write h e l l

bluecrystal
October 12th, 2002, 12:19 AM
you can write follow line into your head file:

extern var_type var;

then you include this head file in all other files.
of curse ,you must be define a global var in your main source file(this file include main() function) : var_type var



:D

dude_1967
October 12th, 2002, 05:24 AM
delic,

bluecrystal has indicated the proper working solution.

It is worthwhile to consider your problem a little bit more...

I have hacked in a sample. I think you might also benefit from the so-called header file guard as shown below.

Chris.

Here is a sample source file:



int my_int_array[128];

int main(int argc, char* argv[])
{
return 1;
}



and here the corresponding header, say my_header.h



#ifndef _MY_HEADER_H_
#define _MY_HEADER_H_

extern int my_int_array[128];

#endif

jfaust
October 12th, 2002, 10:26 AM
It is worthwhile to consider your problem a little bit more...


It definitely is worth considering more. Consider not doing it.

It would be much better to add to global methods that return the values. It would be even better to do away with the globals altogether. It's not always possible to do this, but it usually is, and a little bit of design work can go a long way.

Jeff

delic
October 12th, 2002, 11:28 AM
jjfaust I agree but I am not coding a new program I am altering an old C openGL app I do not want to recode the whole program.. THat would be a nightmare,plus I do not have the hours to do so..

Thank you so much for the help guys..