Global arrays shared between multiple source files

This is a discussion on Global arrays shared between multiple source files within the C Programming forums, part of the General Programming Boards category; I have a couple of global arrays that I want to be accessible in a few source files, but I ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Global arrays shared between multiple source files

    I have a couple of global arrays that I want to be accessible in a few source files, but I cant get it to work. First I tried using a shared header:
    Code:
    #ifndef HEADER_SEEN
    	#define HEADER_SEEN 
            //In here
    #endif
    Which seems to work for my #defines and struct definitions, but if I put the arrays in there gcc moans about multiple definitions.

    If I put the code in main then the other source files cant see it. I dont know if this makes a difference but I am assigning values to the arrays as I declare them.

    How can I fix this?

    Cheers.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Tell the linker they exist elsewhere...

    globals.h
    Code:
    #ifndef INCLUDED_GLOBALS_H
    #define INCLUDED_GLOBALS_H
    
    extern int myVar;
    
    #endif /* INCLUDED_GLOBALS_H */
    main.c
    Code:
    #include "globals.h"
    #include "something.h"
    
    int myVar = 10;
    
    int main(void)
    {
        printf("myVar = %d\n", myVar);
        something_whatever();
        printf("myVar = %d\n", myVar);
    
        return 0;
    }
    something.c
    Code:
    #include "globals.h"
    
    void something_whatever(void)
    {
        myVar = 50;
    }
    You could also add set/fetch methods in main.c and call them from something.c (that way you can control how and who can set it). Otherwise it's a tad "dangerous"

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    THanks zacs, that seems to work, but I dont seem to be able to initialise them as they are declared. Is there any way that I can do this?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why not? Remember it's declared in main.c:

    main.c
    Code:
    char myArray[128] = "Initial value";
    
    void main(void)
    {
        /* ... */
    globals.h
    Code:
    #ifndef INCLUDED_GLOBALS_H
    #define INCLUDED_GLOBALS_H
    
    extern char myArray[128];
    
    #endif /* INCLUDED_GLOBALS_H */

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh, I found what was wrong; I had them declared as const datatypes.

    Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex across multiple source files
    By Quasar in forum Linux Programming
    Replies: 7
    Last Post: 12-04-2007, 07:25 AM
  2. WM_COPYDATA and mutex selecting multiple files
    By gh0st in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2006, 02:22 PM
  3. Beginner Question: Multiple source files
    By ironfistchamp in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2006, 02:19 PM
  4. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM
  5. building source files?
    By Calef13 in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2006, 10:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21