Thread: Macro/globals question

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    8

    Macro/globals question

    Hi,

    I have a globals header that contains all my global variables and constants. The basic layout looks like this:
    Code:
    #ifndef _GLOBALS_H_
    #define _GLOBALS_H_
    
    int foo;
    
    const int bar = 2;
    
    #endif
    Obviously I include this header in a lot of different files. The loader is complaining that there are multiple definitions of bar but does not complain about foo. The problem is obviously that I'm including globals.h in two different files, but if I add static in front of const int bar, doesn't that create separate global variables for the two different files? Also, why doesn't the loader complain about foo being defined multiple times? Thanks in advance.

    EDIT: Fixed typo.
    Last edited by newlearner; 07-18-2009 at 01:05 AM.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    #ifndef _GLOBALS_H_
    #define _GLOBAL_H_
    What this translates too is

    If _GLOBALS_H is not defined
    Define _GLOBAL_H_

    So, any time the compiler reaches this statement, _GLOBALS_H will never be defined so it will go ahead and define _GLOBAL_H_

    Inclusion guards always need to be of the same name.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    8
    Sorry my mistake....typo in writing out code. Thanks for catching that.

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Sorry my mistake....typo in writing out code. Thanks for catching that.
    Are you still getting an error ?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, the OP is.
    Obviously, the same variable is created in each source file that includes the header, so it's natural for multiple definition errors.
    What you need to do is place the globals in ONE .c file, then put "extern" in front of those variables in the headers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    8
    @Spidey, yes I meant I made the typo while typing the post. Sorry for not being clear.
    @Elysia, excellent trick. I was looking through some source code of other projects and found a way to make your trick even better. I found this in the source code for mutt.
    Code:
    #ifdef MAIN
    #define EXTERN
    #else
    #define EXTERN extern
    #endif
    Now define all your globals in globals.h as:
    Code:
    EXTERN int bar;
    Then in main, define MAIN. This makes it slightly better because now all your global variables are defined in main, and everywhere else they are defined as extern. Now you don't need an extra source file to contain all your globals.

    I still would like to know why it doesn't complain about foo when it complains about bar...

    EDIT: added extra code for clarification

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Globals can be anywhere you want. In main, in a separate source file or in an existing source file, so long as it is defined only once.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    8
    Yes, but why did the loader only complain about "bar" being defined multiple times and not "foo"?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by newlearner View Post
    Yes, but why did the loader only complain about "bar" being defined multiple times and not "foo"?
    Because your declaration of foo had no modifiers (extern, etc). It's definition is therefore local to each translation unit. In other words, each source file that #include's your header receives a variable named foo that is only visible to functions in that source file, and distinct from the variables named foo visible to other source files.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Yes, but why did the loader only complain about "bar" being defined multiple times and not "foo"?
    I would think that this would have something to do with the symbol-table the compiler generates for variables.

    Anytime the compiler comes across a declaration which already exists in the table, it throws an error. However, there are a few cases where a symbol table entry is not added -
    These would be typedefs, structs, classes templates ,function prototypes ,extern variables and a few more I cant remember.

    You declared foo as an int however the default linkage for global variables in C is extern. So a symbol table entry was not created. However since you declared bar as a const, it was added to the symbol table until you explicitly declared it as extern.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM