CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2001
    Location
    US,NY
    Posts
    25

    How to create global variables available functions in separate files??

    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
    Last edited by delic; October 11th, 2002 at 09:30 PM.

  2. #2
    Join Date
    Sep 2002
    Posts
    4

    Talking

    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



    nothing to do........
    nothing to say.......

  3. #3
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    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:

    Code:
    int my_int_array[128];
    
    int main(int argc, char* argv[])
    {
      return 1;
    }
    and here the corresponding header, say my_header.h

    Code:
    #ifndef _MY_HEADER_H_
      #define _MY_HEADER_H_
    
      extern int my_int_array[128];
    
    #endif
    You're gonna go blind staring into that box all day.

  4. #4
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  5. #5
    Join Date
    May 2001
    Location
    US,NY
    Posts
    25
    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..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured