Jump to content
  • Advertisement
Sign in to follow this  
Niddles

Problems with multiple definitions

This topic is 4526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone! I am now working on a tetris clone after finishing both pong and breakout. I decided to break this project up into several source and header files, due to the amount of lines of code. I have come across a problem that the compiler is throwing multiple definitons of a bunch of extern const types:
//globals.h
#ifndef _GLOBALS_H
#define _GLOBALS_H
#include <windows.h>
#include <GL/gl.h>
#include <ctime>
#include <cstdlib>

int WindowHeight = 600;
int WindowWidth = 600;
char* AppName = "Tetris";
enum BlockShape {SQUARE, L, S, T, I};
enum CurrentBlockPosition {STARTING, FALLING};
float boxwidth = 0.06;
float boxheight = 0.06;
float startingmidx = -0.35;
float startingtopy = 0.76;
#endif



That is what the header file looks like.It throws "multiple definitions" from WindowHeight all the way down to startingtopy. Besides that problem, it's throwing linker errors about many undefined references to my class "GameBoard". Here's what this code looks like.
//board.h
#ifndef _BOARD_H
#define _BOARD_H
#include "block.h"
class board
{
    public:
        bool blockThere[20][30];
        block* nextblock;
        block* currblock;
        CurrentBlockPosition currBlockPos;
        void drawBack();
};
extern board GameBoard;
#endif



//board.cpp
#include "board.h"
void board::drawBack()
{
    glBegin(GL_QUADS);
        glColor3f(0.0, 0.0, 0.0);
        glVertex2f(-0.95, 0.9);
        glVertex2f(-0.95, -0.9);
        glVertex2f(0.25, -0.9);
        glVertex2f(0.25, 0.9);
    glEnd();
    glBegin(GL_QUADS);
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex2f(0.35, 0.9);
        glVertex2f(0.35, 0.6);
        glVertex2f(0.65, 0.6);
        glVertex2f(0.65, 0.9);
    glEnd();
}



//game.cpp
#include "board.h"
#include "game.h"
void registerWndClass(HINSTANCE hInstance, const char* name, WNDCLASS &wc, int height, int width, HWND &hWnd)
{
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = name;
    RegisterClass (&wc);
    
    hWnd = CreateWindow (name, name, WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
                         0, 0, width, height, NULL, NULL, hInstance, NULL);
}
void gameInit()
{
    GameBoard.currBlockPos = STARTING;
    srand(time(NULL));
}
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;
    *hDC = GetDC (hWnd);
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW |
      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}
void gameRun(HDC hDC)
{
    glClearColor(0.0f, 255.0f, 255.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    GameBoard.drawBack();
    if(GameBoard.currBlockPos == STARTING)
    {
        GameBoard.currblock = GameBoard.nextblock;
        GameBoard.nextblock = new block(false);
        GameBoard.currBlockPos = FALLING;
    }
    if(GameBoard.currBlockPos == FALLING)
    {
        if(!GameBoard.currblock->atBottom())
        {
            GameBoard.currblock->moveBlockDown();
        }
        else
        {
            GameBoard.currBlockPos = STARTING;
        }
    }
    glPopMatrix();
    SwapBuffers(hDC);
    Sleep(1);
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam)
{

    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}
int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    bool playing = true;

    registerWndClass(hInstance, AppName, wc, WindowWidth, WindowHeight, hWnd);


    EnableOpenGL (hWnd, &hDC, &hRC);
    gameInit();

    while (playing)
    {

        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {

            if (msg.message == WM_QUIT)
            {
                playing = false;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
            gameRun(hDC);
        }
    }


    DisableOpenGL (hWnd, hDC, hRC);


    DestroyWindow (hWnd);

    return msg.wParam;
}



There are the files that use the class board and "GameBoard." Can someone help me figure out these puzzling errors please? [Edited by - Niddles on May 30, 2006 1:06:28 AM]

Share this post


Link to post
Share on other sites
Advertisement
For starters, when you define a variable as 'extern', you're basically saying to the compiler, "This is going to be defined somewhere else - I'm just letting you know it exists." You must also declare the variable in a cpp file, without the 'extern' keyword.

So, for instance, at the top of board.cpp, you should have:


// stuff...

board GameBoard;

// more stuff ...




That will actually define the variable you have declared, and it will resolve the previously unresolved external 'GameBoard'

I don't know if your multiple definition errors are related, but fix this first and see what happens.

Share this post


Link to post
Share on other sites
That got rid of undefined references to GameBoard, but I still have multiple definitions, and now an undefined reference to registerWndClass.

Share this post


Link to post
Share on other sites
Fixed undefined reference to registerWndClass, but still multiple definition errors.

Errors are as follows:

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\New Folder\The Tetris\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\New Folder\The Tetris\Makefile.win" all
g++.exe block.o board.o game.o -o "TheTetris.exe" -L"lib" -mwindows -lopengl32

board.o(.data+0x0):board.cpp: multiple definition of `WindowHeight'
block.o(.data+0x0):block.cpp: first defined here

board.o(.data+0x4):board.cpp: multiple definition of `WindowWidth'
block.o(.data+0x4):block.cpp: first defined here
board.o(.data+0x8):board.cpp: multiple definition of `AppName'
block.o(.data+0x8):block.cpp: first defined here
board.o(.data+0xc):board.cpp: multiple definition of `boxwidth'
block.o(.data+0xc):block.cpp: first defined here
board.o(.data+0x10):board.cpp: multiple definition of `boxheight'
block.o(.data+0x10):block.cpp: first defined here
board.o(.data+0x14):board.cpp: multiple definition of `startingmidx'
block.o(.data+0x14):block.cpp: first defined here
board.o(.data+0x18):board.cpp: multiple definition of `startingtopy'

block.o(.data+0x18):block.cpp: first defined here
game.o(.data+0x0):game.cpp: multiple definition of `WindowHeight'
block.o(.data+0x0):block.cpp: first defined here
game.o(.data+0x4):game.cpp: multiple definition of `WindowWidth'
block.o(.data+0x4):block.cpp: first defined here
game.o(.data+0x8):game.cpp: multiple definition of `AppName'
block.o(.data+0x8):block.cpp: first defined here
game.o(.data+0xc):game.cpp: multiple definition of `boxwidth'
block.o(.data+0xc):block.cpp: first defined here
game.o(.data+0x10):game.cpp: multiple definition of `boxheight'
block.o(.data+0x10):block.cpp: first defined here
game.o(.data+0x14):game.cpp: multiple definition of `startingmidx'
block.o(.data+0x14):block.cpp: first defined here
game.o(.data+0x18):game.cpp: multiple definition of `startingtopy'
block.o(.data+0x18):block.cpp: first defined here
collect2: ld returned 1 exit status

make.exe: *** [TheTetris.exe] Error 1

Execution terminated

Share this post


Link to post
Share on other sites
word of advice never use varibles in a header file it a head ach so i would copy them all over to the source file *note that they can all still be used in other files if global

Share this post


Link to post
Share on other sites
Well, why don't you make the variables extern too?
like this:

//globals.h
#ifndef _GLOBALS_H
#define _GLOBALS_H
// why are you including all this?
// this header file doesn't need any of the stuff from those headers...
//#include <windows.h>
//#include <GL/gl.h>
//#include <ctime>
//#include <cstdlib>

enum BlockShape {SQUARE, L, S, T, I};
enum CurrentBlockPosition {STARTING, FALLING};
extern int WindowHeight;
extern int WindowWidth;
extern char* AppName;
extern float boxwidth;
extern float boxheight;
extern float startingmidx;
extern float startingtopy;
#endif




//globals.cpp
#include "globals.h"

int WindowHeight = 600;
int WindowWidth = 600;
char* AppName = "Tetris";
float boxwidth = 0.06;
float boxheight = 0.06;
float startingmidx = -0.35;
float startingtopy = 0.76;



this should work.

Share this post


Link to post
Share on other sites
Alright I did that, but it's still giving me the same error. The reason I'm including all of those headers is so I don't have to include them many times down the line.

Edit:
I rebuilt the project instead of just compiling, it works now, but I have a leak somewhere, windows is giving me "The Tetris.exe has encountered a problem and has to close now" error.
Meh I'll figure it out. Thanks for the help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!