Thread: globals & multiple .c files

  1. #1
    Unregistered
    Guest

    globals & multiple .c files

    hi,
    i wrote a rather large program which's source i'm now trying to organise a bit cleaner. since this is the first time i use *.c files for code and *.h for prototypes, i'm having a bit of trouble with gcc...
    i need global variables (stored in "defs.h") for the .c file with main and three others. if i have all these c-files include defs.h, gcc complains about multiple declarations of those variables, otherwise it says they're undeclared.
    my defs.h file looks like:

    #ifndef DEFS__
    #define DEFS__
    int x, y, z, blah...;
    char foobar[346];
    #endif

    the c file with main() in it includes "main.h". "main.c" includes defs.h, too, and reads user input into those variables. functions from two other .c modules are then called, that use these variables (as does main())- they both include "defs.h", too. if i remove the include from any of these files, i get that "undeclared variable" error.
    to make it short: how can i just use globals for multiple .c modules?
    any help very appreciated

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The keyword 'extern' is your friend.

    FileA:

    int myglobal;

    FileB:

    extern int myglobal;

    This basicly just creates a reference point to the original variable.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User FCF's Avatar
    Join Date
    Dec 2001
    Posts
    40
    Dear quzah,
    Sorry to interrupt, i think i have to ask something.
    Can u describe a bit the usage of "extern" in practical? I read my reference book and i can't understand clearly.
    Thank u first for your help.

  4. #4
    Unregistered
    Guest
    quzah, excellent - thanks. i would have never thought of that.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by FCF
    Dear quzah,
    Sorry to interrupt, i think i have to ask something.
    Can u describe a bit the usage of "extern" in practical? I read my reference book and i can't understand clearly.
    Thank u first for your help.
    Sure. Let's assume that I have a table of items in a file:
    Code:
    struct vehicle {
        char *name;
        int weight;
        int color;
        float mpg;
    } vehiclelist[] =
    {
        { "Ferarri", 2000, COLOR_RED, 20.1  },
        { "Limo", 5000, COLOR_BLACK, 15.3 },
        { "Econo-car", 1200, COLOR_BLUE, 55.7 },
    };
    Ok, good enough. (If you're wondering, I declared a structure "struct vehicle", defined it, and followed that by creating a table (array) of these structures, called "vehiclelist")

    Let's assume this list has a ton of entries.
    Let's assume I'm using them in a progject that spans multiple .c files.

    Now, I have one .c file called "output.c" that handles all of my "output" functions. This may be to disk, or perhaps to the screen, whatever. Anything considered output is all in this file.

    Now, assuming I actually want to display any information on my vehicles, (which are contained in a file called "vehicle.c"--that has all of my vehicle related functions--getting speed, breaking, calculating impact damage, whatever), my 'output.c' file needs to know about this table.

    Thus, in 'vehicle.c', the table is actually created, it exists there.

    In 'output.c', I need a reference to that variable/table:

    extern struct vehicle vehicletable[];

    Now it can see the table.

    Quzah.
    Last edited by quzah; 07-03-2002 at 08:19 PM.
    Hope is the first step on the road to disappointment.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Can also do

    FileA:

    #include "def.h"

    FileB:
    FileC:
    FileD:
    extern int myglobal;

    (def.h not included in these files but a variables declared in def.h can be declared here as an extern)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    quzah,

    a bit of question, is extern on the

    >> extern void function (int *)
    and
    >> void function (int *)

    same?? or it might be have some adv and disadv on each statements

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The extern keword is really a tag which states "Whatever follows is defined in some other file."

    Thus:

    extern int x;

    Doesn't actually create a variable called 'x', it creates a sort of reference to another variable which is defined elsewhere. It's really a reference for the compiler so it knows what you're doing. You use it like you would that actual variable, because in effect you are actually using that variable.

    The same thing goes for prototyping functions. You're saying "Hey, this function isn't defined here, but it's in some other file and it takes this format, so it's valid for me to use it this way."

    In effect it makes a global reference (global in the true sense of the word, not limited to a single file, but the entire program) to whatever follows it, so you can use that item.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User FCF's Avatar
    Join Date
    Dec 2001
    Posts
    40
    I saw an example in my note, the program is a linked list program.
    Code:
    ]
    #include <stdio.h>
    #include "ListInterface.h" 
    int main()
    {
    ...
    }
    In the ListInterface.h, there are something like declaring global variables, structures, and etc. But, there is still another file called ListImplementation.cpp. In the ListImplememtation.cpp, it #include "ListInterface", and includes many function prototypes like this:
    Code:
    extern int insert(Nodeptr L, ItemType X);
    ...
    If follows wat quzah said:
    Whatever follows is defined in some other file
    , why we use extern in the ListImplamentation.cpp? Becaz no insert function to be refered outside.

    I follow the note and got some errors when compiling: ...function need to be resolved. But the error fixed when i replaced the #include "ListInterface.h" with #include "ListImplementation.cpp" file in my main program.
    WHY???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. need assistance with multiple files
    By c++.prog.newbie in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2006, 01:44 AM
  3. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  4. Difference between .h and .c files
    By Metalix in forum C Programming
    Replies: 9
    Last Post: 01-19-2005, 08:38 PM
  5. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM