Thread: Blah, what am I doing wrong?

  1. #1
    Unregistered
    Guest

    Blah, what am I doing wrong?

    I've come to the conclusion that I just can't split a program up into multiple files and come away with it working.

    I'm getting linking errors and "not declared" errors all over the place using DevC++

    I have the following files:

    globals.h
    main.h
    core.h

    main.cpp
    core.cpp

    Here's the basic contents of the files (removed most of the source since the problem is a linker error).

    ------------------------------------------------
    // GLOBALS.H
    #ifndef _GLOBALS_H
    #define _GLOBALS_H

    extern HWND g_hwndMain;
    extern LPSTR g_lpszClassName;

    #endif
    ------------------------------------------------
    // MAIN.H
    #ifndef _MAIN_H
    #define _MAIN_H

    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>

    #endif
    ------------------------------------------------
    // CORE.H
    #ifndef _CORE_H
    #define _CORE_H

    #include "globals.h"
    #include "main.h"

    // function prototypes
    HWND CreateMainWindow(HINSTANCE hInstance);
    ATOM RegisterMainClass(HINSTANCE hInstance);

    #endif
    ------------------------------------------------
    //MAIN.CPP

    #include "globals.h"
    #include "main.h"
    #include "core.h"

    HWND g_hwndMain = NULL;
    LPSTR g_lpszClassName = "myCLASSname";

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {

    // THE CODE

    }
    ------------------------------------------------
    //CORE.CPP

    #include "core.h"

    HWND CreateMainWindow(HINSTANCE hInstance)
    {

    //CODE

    }
    ATOM RegisterMainClass(HINSTANCE hInstance)
    {

    //CODE

    }

    Anyone have any helpful pointers?

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If the code that devc++ provides works, then I can't see what the problem is.

    Could you be more specific as to what the problem is.
    Post some errors/warnings.

    btw have u specified that you want to create a windows app?

  3. #3
    Unregistered
    Guest
    Majority of the errors look like this:

    74 C:\Program Files\Dev-C++\Projects\core.cpp `g_lpszClassName' undeclared (first use this function)

    or like this:

    6 C:\Program Files\Dev-C++\Projects\main.cpp In file included from core.cpp

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Make sure you're not trying to use a variable in core.cpp that was declared in main.cpp.

    That's all the help I can give right now.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    remove the include of globals.h from the core.h header. Include it only in the main.cpp

    Declare the variables as extern in all the files it is needed (including main.cpp)
    ie;

    extern HWND g_hwndmain;// it is declared in globals.h so is extern everywhere else



    I would also declare the HINSTANCE as global, just my preference.
    Last edited by novacain; 07-18-2002 at 09:39 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why does my program print off the wrong answers?
    By kl3pt0 in forum C Programming
    Replies: 18
    Last Post: 06-10-2004, 06:52 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM