This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Global Definitions

Hello!

What is the best method to declare some variables global ( more than one .C File should know it ) and still avoid multiple public definitions?

A Global Header File is definitely NOT the key to success ;-)

Thanks for your help!

  • myinclude.h

    #ifndef _MYINCLUDE_H
    #define _MYINCLUDE_H
    
    #ifdef MAIN
    #define EXTERN
    #else
    #define EXTERN extern
    #endif
    
    EXTERN int my_variable;
    
    #endif _MYINCLUDE_H
    

    main.c


    #define MAIN
    #include "myinclude.h"
    ...

    other.c

    #include "myinclude.h"
    ...
    

  • "A Global Header File is definitely NOT the key to success"

    Oh yes it is!

    Why do you say it isn't?

  • c-faq.com/.../decldef.html

    "It's not just a good idea to put global declarations in header files: if you want the compiler to be able to catch inconsistent declarations for you, you must place them in header files."

  • Thanks a lot!

    This way it is working ;-)

    However, i donÂ't like this solution very much. In my opinion things like this are bad for the readability of code.

    But it is working and that is the most important thing :-)

    Kind regards.

  • Exactly how does it affect the readability?

  • "In my opinion things like this are bad for the readability of code."

    Things like what?

    What exactly is it that you find "not readable"?

    If you explain what you mean, people may be able to offer suggestions; if you don't, they can't - other than say, "It looks fine to me!"

    "Readability" is a subjective issue, so you need to explain what you mean by it!

    Personally, I don't do it the way Per does - but, without knowing what you don't like about that, how do I know if you might like "my" way any better?

  • A Global Header File is definitely NOT the key to success

    The correctness of that depends quite a lot on what exactly you mean by "A Global Header File": a single file, commonly called "globals.h", that declares all globals in the entire program is indeed a bad idea. The only usual exception is if the project is a library, and it's the public header file of that library.

    Declarations of global variables belong in per-module header files. Globals are part of the interface of a module, and that means their declaration belong in the interface description: the corresponding header file.

  • Most often when I have a global variable, I do like the above. It might be a tick counter or similar that a lot of code would like to be able to access.

    When talking about modules, I often have an accessor function (or in case of C++ often a static factory method that returns a single unique object instance). So, the module does not contain any global variables - only static variables (declared with static keyword or in nameless namespace).

    However, as Andy notes, alternatives can not be discuessed without first clearly state goals or usability requirements.