Headers and Global Variables

This is a discussion on Headers and Global Variables within the C++ Programming forums, part of the General Programming Boards category; Hi. I'm with a big problem. I decided to divide my program in several source files, but i've never done ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    Headers and Global Variables

    Hi. I'm with a big problem. I decided to divide my program in several source files, but i've never done it.
    I had a Main.cpp file. Then I created two more source files (file1.cpp and file2.cpp). I divided the functions thematically, letting the main file only with the WinMain and the WndProc functions.
    Then I created a Header file and I put there the Global Variables and the Function Prototypes.
    All my source files must have access to the Global Variables but I don't know how I can do this.
    If I #include the header file it presents a compiler error saying that I'm redifining the variables.
    If I #include the header file only in the Main I get an error from the other source files complaining that they can't access the global variables.
    If I create like a chain, this is: in the Main.cpp I #include the source1.cpp, in the source1.cpp I include source2 and there i include the header I also get an error.

    This may seem like stupid but I would like to know how to do this(if it can be done).

    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    > Then I created a Header file and I put there the Global Variables and the Function Prototypes.
    You can only put extern global variables in header files

    If you previously had
    int myglobal = 0;

    Then what goes in your header file is
    extern int myglobal;

    The int myglobal = 0; you leave where you originally had it.

    After that, you #include your header file in your 3 source files.

    Finally, in your project setting, you list your 3 source files, so that you end up doing (via your IDE project)
    CC Main.cpp file1.cpp file2.cpp
    That is, you should see your IDE compile 3 files, then link the respective object files to produce an executable.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Headers
    By marrk in forum C Programming
    Replies: 8
    Last Post: 11-06-2006, 02:41 PM
  3. code does not compile
    By aldajlo in forum C++ Programming
    Replies: 19
    Last Post: 10-13-2004, 02:54 PM

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