Thread: Messing with header files

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Messing with header files

    I have two .cpp files in my project. (Dev-C++, WinXP)
    They both include "main.h" header.
    No matter if the header contains:
    Code:
    char hello[] = "Hello";
    or
    Code:
    #pragma once
    char hello[] = "Hello";
    or
    Code:
    #ifndef MYHEADER
    #define MYHEADER
    char hello[] = "Hello";
    #endif
    I always get such errors:
    Code:
    multiple definition of `hello'
    first defined here
    And why are those quotes so freaky?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Because you should have in the header file
    Code:
    extern char hello[];
    And in ONE .cpp file,
    Code:
    char hello[] = "hello";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Is that meant for every data type, IE:
    header:
    Code:
    extern int baa;
    ONE .cpp file:
    Code:
    int baa;
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Right - for every global variable that you want to use in several c-files
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Then I will make one header.cpp and main.h.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You can create something like global.cpp with all global variables you want
    and global.h that contains extern for each such variable...

    I personally prefer to build a struct, that includes as a members all global variables I need
    So I will have only one global variable and only one extern...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you have any global variables at all, they all should be in main.cpp (ideally static), and everything else gets access to them via parameters or return results.

    A wide scattering of globals used for all sorts of purposes is poor design.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    ideally static
    ideally they should be local variables inside main()
    But we don't live in the perfect world
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For constants and such, you could also put a static global variable in the header file:
    Code:
    static const char *version = "program version 1.00";
    This generates a new static variable for each file that includes the header.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    They (static) have not to be constant. I don't see a reason for using this, but you can put line

    Code:
    static int i =0;
    in the header and you will have in each c-file including this header its own static int i that can be changed inside this c-file.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    But if they aren't constant they will behave differently than global variables, since modifying one in a source file will not affect those used by other source files.

    BTW, use of static for this purpose is deprecated in favor of the unnamed namespace.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But the unnamed namespace in headers is strongly discouraged by many groups (such as Boost) because it causes problems with precompiled headers on quite a few compilers.

    It's also not needed for constants.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    If you have any global variables at all, they all should be in main.cpp (ideally static), and everything else gets access to them via parameters or return results.
    Now that's an idea! Make global variables that can't be accessed as global variables. Scattering is the point of global variables.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And the curse. Limit their use as much as you can.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You'll often find, especially with good use of encapsulation and OOP, that global variables are completely unnecessary; they often can be replaced with private member variables.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. more header files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2001, 01:56 PM