Thread: Is this how you use extern? Confirmation

  1. #1
    Shadow12345
    Guest

    Is this how you use extern? Confirmation

    Put extern type name(extern HWND hWnd) in a header (main.h or something) and then re-declare the same variable (minus the extern keyword) in every source file you use it in?

    I need to put some text to a window referencing to a HWND object but for some reason it isn't working and I'm wondering if I'm using extern correctly.

    Thanks.
    Last edited by Shadow12345; 12-24-2002 at 07:58 PM.

  2. #2
    Shadow12345
    Guest
    are you sure? I saw it being used the way I described in a tutorial (extern in header, re declared in source file(s)). When i take out these redeclarations I get compile errors so it doesn't seem correct, unless I am doing something else wrong.

    EDIT:
    I got it working. I have extern in main.h, then in one of the source files I redeclare hWnd, but in the other source file I don't redeclare hWnd, but in the one I don't redeclare hWnd i successfully called setwindowtext passing hWnd as the first parameter...this is really weird, anymore suggestions?
    Last edited by Shadow12345; 12-24-2002 at 08:21 PM.

  3. #3
    Shadow12345
    Guest
    Do you have an extern declaration in every other source file (e.g. in a header you include in them)? What are your errors?
    I have the extern declaration in main.h, and I have main.h included in each of the source files, this setup works when I have the redeclaration in one of the source files. When I take out the redeclarations altogether I get the following linker errors that basically say hWnd cannot be found:
    MS3DLoad.obj : error LNK2001: unresolved external symbol "struct HWND__ * hWnd" (?hWnd@@3PAUHWND__@@A)
    Lesson1.obj : error LNK2001: unresolved external symbol "struct HWND__ * hWnd" (?hWnd@@3PAUHWND__@@A)
    Debug/lesson1.exe : fatal error LNK1120: 1 unresolved externals
    Strange huh

    edit:
    contents of Main.h
    Code:
    #ifndef MAIN_H
    #define MAIN_H
    typedef unsigned char byte;
    typedef unsigned short word;
    #include <windows.h>		// Header File For Windows
    #include <gl\gl.h>			// Header File For The OpenGL32 Library
    #include <gl\glu.h>			// Header File For The GLu32 Library
    #include <gl\glaux.h>		// Header File For The Glaux Library]
    #include <fstream.h>
    #include <stdio.h>
    #include "MS3D.H"
    extern HWND hWnd;
    void LoadModel(char *);	//PROTOTYPES SUCK ASS
    #endif
    snippet from one of the source files
    Code:
    #include "Main.h"
    
    void LoadModel(char * fileName) {
    
    ifstream fin;
    fin.open(fileName, ios::in | ios::binary | ios::nocreate);
    if(fin.fail()){
    MessageBox(NULL, "UNABLE TO OPEN MODEL FILE", "ERROR", MB_OK);
    return;
    }
    fin.seekg(0, ios::end);			//SEEK TO END OF FILE
    long filesize = fin.tellg();	//GET SIZE IN BYTES OF FILE
    fin.seekg(0 ,ios::beg);			//SEEK BACK TO BEGINNING
    byte *buffer = new byte[filesize];	//ALLOCATE MEMORY FOR BUFFER
    fin.read(buffer, filesize);			//READ STUFF FROM FILE INTO A BUFFER
    fin.close();	
    
    byte *ptr = buffer;	//CREATE POINTER TO BUFFER
    MS3DHeader *pHeader = (MS3DHeader*) ptr;
    ptr += sizeof(MS3DHeader);
    char numvert[50];
    
    int numverts = *(word*) ptr;
    sprintf(numvert,"Number Verts: %d",  5);
    SetWindowText(hWnd, "**** **** ****");
    snippet from the other source file
    Code:
    #include "Main.h"
    
    HDC			hDC=NULL;		// Private GDI Device Context
    HGLRC		hRC=NULL;		// Permanent Rendering Context
    HWND		hWnd;
    HINSTANCE	hInstance;		// Holds The Instance Of The Application
    
    bool	keys[256];			// Array Used For The Keyboard Routine
    bool	active=TRUE;		// Window Active Flag Set To TRUE By Default
    bool	fullscreen=TRUE;	// Fullscreen Flag Set To Fullscreen Mode By Default
    Like I said, strange.
    Last edited by Shadow12345; 12-24-2002 at 08:34 PM.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Shadow12345
    I have the extern declaration in main.h, and I have main.h included in each of the source files, this setup works when I have the redeclaration in one of the source files. When I take out the redeclarations altogether I get the following linker errors that basically say hWnd cannot be found:


    Strange huh

    edit:
    contents of Main.h
    Code:
    #ifndef MAIN_H
    #define MAIN_H
    typedef unsigned char byte;
    typedef unsigned short word;
    #include <windows.h>		// Header File For Windows
    #include <gl\gl.h>			// Header File For The OpenGL32 Library
    #include <gl\glu.h>			// Header File For The GLu32 Library
    #include <gl\glaux.h>		// Header File For The Glaux Library]
    #include <fstream.h>
    #include <stdio.h>
    #include "MS3D.H"
    extern HWND hWnd;
    void LoadModel(char *);	//PROTOTYPES SUCK ASS
    #endif
    snippet from one of the source files
    Code:
    #include "Main.h"
    
    void LoadModel(char * fileName) {
    
    ifstream fin;
    fin.open(fileName, ios::in | ios::binary | ios::nocreate);
    if(fin.fail()){
    MessageBox(NULL, "UNABLE TO OPEN MODEL FILE", "ERROR", MB_OK);
    return;
    }
    fin.seekg(0, ios::end);			//SEEK TO END OF FILE
    long filesize = fin.tellg();	//GET SIZE IN BYTES OF FILE
    fin.seekg(0 ,ios::beg);			//SEEK BACK TO BEGINNING
    byte *buffer = new byte[filesize];	//ALLOCATE MEMORY FOR BUFFER
    fin.read(buffer, filesize);			//READ STUFF FROM FILE INTO A BUFFER
    fin.close();	
    
    byte *ptr = buffer;	//CREATE POINTER TO BUFFER
    MS3DHeader *pHeader = (MS3DHeader*) ptr;
    ptr += sizeof(MS3DHeader);
    char numvert[50];
    
    int numverts = *(word*) ptr;
    sprintf(numvert,"Number Verts: %d",  5);
    SetWindowText(hWnd, "**** **** ****");
    snippet from the other source file
    Code:
    #include "Main.h"
    
    HDC			hDC=NULL;		// Private GDI Device Context
    HGLRC		hRC=NULL;		// Permanent Rendering Context
    HWND		hWnd;
    HINSTANCE	hInstance;		// Holds The Instance Of The Application
    
    bool	keys[256];			// Array Used For The Keyboard Routine
    bool	active=TRUE;		// Window Active Flag Set To TRUE By Default
    bool	fullscreen=TRUE;	// Fullscreen Flag Set To Fullscreen Mode By Default
    Like I said, strange.
    You need something like this. It wouldn't be a problem but you are including that header in a bunch of source files. Lets just say you have your main.h and then a main.cpp and a utility.cpp. Here is what you need to do...

    main.h
    Code:
    #ifndef MAIN_H
    #define MAIN_H
    
    /* Declarations.. etc */
    #ifdef MAIN_CPP
    #define GLOBAL
    #else 
    #define GLOBAL extern
    #endif
    
    GLOBAL HWND hWnd;
    /* Other global variables ... */
    #endif /* MAIN_H */
    Then in your main.cpp..
    Code:
    #define MAIN_CPP
    #include "main.h"
    
    /* Make your assignments as usual... */
    Then in your utility.cpp you don't need to define MAIN_CPP because you want to use the external version.
    Code:
    #include "main.h"
    
    /* Use your variables as usual.. */
    Hope that helps you
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Shadow12345
    Guest
    Actually that helps a lot and seems like a much better way to solve this problem. I like it!! Thank you both

    EDIT: And just to make sure you get the satisfaction I added that to the project and it works (hooray!)
    Last edited by Shadow12345; 12-24-2002 at 09:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern const?Please help
    By huwan in forum C++ Programming
    Replies: 10
    Last Post: 08-12-2008, 04:53 AM
  2. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM