variable declaration in H file

This is a discussion on variable declaration in H file within the C Programming forums, part of the General Programming Boards category; hi forum, i'm new to C and trying to declare some variables to use on global scope a cross .c ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    variable declaration in H file

    hi forum,

    i'm new to C and trying to declare some variables to use on global scope a cross .c files. here is a sample of the code:

    abc.h:
    --------

    int rc;

    int thinkTime = 5;

    string urlName;

    char funcLibPath2[] = "C:\\LR_Func";

    string testPath3 = "C:\\LR_Func";

    -----------

    compiler throws all kind of garbage... primerally ' redefinition of <var name> previously defined at <file path>' error messages.

    what is wrong with these declarations?

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Do you include abc.h in the program more than once? It's a good idea to place inclusion guards in your header files so that there's no way they can be included more than once, even if you try
    Code:
    #if !defined (HEADER_NAME_H)
    #define HEADER_NAME_H
    
    /* Stuff in the header file... */
    
    #endif
    Now you can include abc.h any number of times, but it'll only be actually included once.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Also, I believe you initialize variables in a source file and not a header file.

    abc.h:
    --------

    extern int rc;

    extern int thinkTime;

    extern string urlName;

    extern char funcLibPath2[] ;

    extern string testPath3;

    abc.c:
    --------

    int rc;

    int thinkTime = 5;

    string urlName;

    char funcLibPath2[] = "C:\\LR_Func";

    string testPath3 = "C:\\LR_Func";
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    You will also want to declare those globals as extern.

    --edit--
    You beat me by seconds.
    The keyboard is the standard device used to cause computer errors!

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    If you want a variable to be declared in only a single source file, use the static keyword:

    Code:
    static int MyVariable;
    static int MyVariable2;
    Variables with the static keyword can be placed in a header file, in which case the variable will be defined in all source files that include the header file.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    thx a lot for hints

    i probably will go with static declaration to make architecture centralized.

    what kind of stuff i can include into header?

    also, where would i find good literature about how to in C?

    i.e. :
    - creating .ini files
    - declaring variables
    - samples of basic code, etc. - C road map for someone with programming experience to acquire C basics...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM

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