Thread: extern symbols, what do these mean exactly?

  1. #1
    Shadow12345
    Guest

    extern symbols, what do these mean exactly?

    Here is some code I found that uses extern. This is in a file called main.h, but it is all redeclared in main.cpp, and I am not sure what the purpose of the extern keyword is.
    Code:
    extern CCamera	g_Camera;									// This will be our global camera data
    extern bool  g_bFullScreen;									// Set full screen as default
    extern HWND  g_hWnd;										// This is the handle for the window
    extern RECT  g_rRect;										// This holds the window dimensions
    extern HDC   g_hDC;											// General HDC - (handle to device context)
    extern HGLRC g_hRC;											// General OpenGL_DC - Our Rendering Context for OpenGL
    extern HINSTANCE g_hInstance;

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Those are global variables through all modules in your project (if you have multiple linked files).
    If you don't declare them as extern, they will only be global in the current file.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Shadow12345
    Guest
    Ok that makes sense. Now something that doesn't make sense is those extern statements are in main.h, but the following code is in main.cpp. Why would they have the same things declared twice?

    Code:
    CCamera g_Camera;										// This is our global camera object
    
    bool  g_bFullScreen = true;								// Set full screen as default
    HWND  g_hWnd;											// This is the handle for the window
    RECT  g_rRect;											// This holds the window dimensions
    HDC   g_hDC;											// General HDC - (handle to device context)
    HGLRC g_hRC;											// General OpenGL_DC - Our Rendering Context for OpenGL
    HINSTANCE g_hInstance;									// This holds the global hInstance for UnregisterClass() in DeInit()
    It is just the same thing albet the extern keyword

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You need those declarations in every file so that particular file knows those variables exists. Then you need to tell each file that it is the same variable in all files, and this is done in the header file using extern (you include this header in all files that use the variables).

    <edit>
    After some testing I have a more accurate answer:

    You need the variables to be defined in all cpp files that use them. All of them should be defined as extern but one (that's the one holding the actual data). It doesn't matter which file you place the 'real' one in. The rest of them (the extern ones) will refer to that variable when accessing them.

    I found out that you don't need to put a declaration in the header file. It works fine without it (Could be my compiler or something else though).
    Code:
    //LinkHeader.h
    
    void SetVariable();
    Code:
    //LinkBody1.cpp
    
    #include <iostream.h>
    #include <conio.h>
    #include "LinkHeader.h"
    
    int GlobalVar;
    
    int main()
    {
       GlobalVar = 0;
       SetVariable();
       cout << GlobalVar << endl;
       getch();
       return 0;
    }
    Code:
    //LinkBody2.cpp
    
    #include "LinkHeader.h"
    
    extern int GlobalVar;
    
    void SetVariable()
    {
       GlobalVar = 123456;
    }
    </edit>
    Last edited by Magos; 11-02-2002 at 10:47 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    13
    like Magos allready mentioned, an extern declaration like
    Code:
    extern CCamera g_Camera;
    doesn't create the CCamera object, it just brings an elsewhere declared object in scope again. Those statements are soley declarations, in contrast to
    Code:
    CCamera g_Camera;
    which does create a CCamera object and makes it available' as "g_Camera"; it's a declaration + definition.

    In your case there is only one CCamera object. The declaration + definition (that means memmory alloc + initialisation) in main.cpp forces the compiler to create it.
    The extern declaration in all other files just refer to that object.
    Teneniel
    "The Frenchmen and Russians possess the land, the British possess the sea, but we have over the airy realm of dreams command indisputably." ~ Heinrich Heine

  6. #6
    Shadow12345
    Guest
    Okay this idea is less hazy. So I need the 'actual' declaration in one file, lets just call this main. Then I have other source files that need to use that one instance in my 'main.cpp' file.

    so in main.cpp:
    //This is the 'actual' thing being declared + defined
    vector<Triangle*> TriangleVector;

    'othersource.cpp'
    //This just refers to the actual data in main.cpp
    #include "Includes.h" //everything i need including vector
    extern vector<Triangle*> TriangleVector;

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Was that a question?
    Yes, it should work (read: try it). You'd need to include the headers for vector in main.cpp too, but I guess you just missed that .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Shadow12345
    Guest
    That was just me making sure I understood it. I actually have a single header that includes all of the other headers, and I just include that whenever I need it.

    How do you make sure you don't include a header twice?

    isn't it something like

    #ifndef CLASS_DECLARATIONS_H
    #define CLASS_DECLARATIONS_H
    #endif

    if the header name is ClassDeclarations.h

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    That's correct!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Trouble with Windows/DirectX programming
    By bobbelPoP in forum Windows Programming
    Replies: 16
    Last Post: 07-08-2008, 02:27 AM
  3. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Strange error?
    By MrLucky in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2006, 03:01 PM