0

So, I'm new to C++ and SDL... And I get the linking error : (in SDL_Game.exe, the compiled version of these codes)

LNK 1120 : Unresolved Externals

And I also get these 2 errors :

Error   2   error LNK2001: unresolved external symbol "public: static bool SDL_main::isRunning" (?isRunning@SDL_main@@2_NA) C:\Users\MyName\Desktop\SDL_Game\SDL_Game\Input.obj SDL_Game


Error   3   error LNK2001: unresolved external symbol "public: static bool SDL_main::isRunning" (?isRunning@SDL_main@@2_NA) C:\Users\MyName\Desktop\SDL_Game\SDL_Game\main.obj  SDL_Game

My application type is set to console, so that's not the problem.

  • I probably did something really stupid...

(Note : There's probably a lot of mistakes in here, you have been warned...)

So, here's my source codes :

main.cpp :

#include "Graphics\Window.h"
#include "Input\Input.h"
#include <string>
#include <iostream>
#include <main.h>

void Debug(std::string text);

int main(int argc, char* argv[])
{
    main::isRunning = true;
    Window C_Window;
    Window window();
    Input input();

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        main::isRunning = false;
        std::cout << "SDL Video Initialization Failed : " << SDL_GetError() << std::endl;
        std::cout << "Press enter to quit..." << std::endl;
        system("PAUSE"); // Only works on windows.

    }
    else 
    {
        while (main::isRunning)
        {
            SDL_UpdateWindowSurface(C_Window.GetWindow());
        }
    }
    SDL_UpdateWindowSurface(C_Window.GetWindow());
    Debug("SDL_winsurupd called in main");
    system("PAUSE"); // Only works on windows.
    SDL_Quit();

    return 0;
}

void Debug(std::string text)
{
    std::cout << text << std::endl;
}

main::~main()
{

}

main.h :

#pragma once
#include <SDL.h>
#include <iostream>
#include <Graphics\Window.h>

class main
{
public:

    static bool isRunning;

    main();

    ~main();

private:



};

Window.h

#pragma once
#include <SDL.h>
#include <iostream>

class Window
{
public:

    Window();

    SDL_Window* GetWindow();

    ~Window();

private:

    SDL_Window* window = nullptr;

};

Window.cpp

#include "Window.h"


Window::Window()
{
    window = SDL_CreateWindow("Engine Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    if (window == NULL)
    {
        std::cout << "Window Creation Failed : " << SDL_GetError() << std::endl;
        system("PAUSE"); // Only works in windows.
    }
    else 
    {
        SDL_UpdateWindowSurface(window);
    }
}

SDL_Window* Window::GetWindow()
{
    if (window != NULL)
    {
        return window;
    }
    else 
    {
        std::cout << "Could not get window since it was not created yet." << std::endl;
        system("PAUSE");
    }
}


Window::~Window()
{
}

Input.h

#pragma once
#include <SDL.h>
#include <iostream>
#include <Graphics\Window.h>

class Input
{
public:

    Input();

    ~Input();

private:

    SDL_Event evnt;

};

Input.cpp

#include "Input.h"
#include <main.h>


Input::Input()
{
    while (main::isRunning)
    {
        while (SDL_PollEvent(&evnt) != 0)
        {
            if (evnt.type == SDL_QUIT) {
                SDL_Quit();
            }
        }
    }
}


Input::~Input()
{

}
Happy Crayfish
  • 47
  • 1
  • 11
  • It is a very bad idea to have a class named `main` as that is the name of the main function in your program. I would suggest you change the name and try to compile again. – NathanOliver Oct 26 '15 at 12:42
  • @NathanOliver Renamed the class to core, still the same errors :/ – Happy Crayfish Oct 26 '15 at 12:47
  • Trust me, this error comes up almost daily. You only *declare* the static member variable `isRunning`, you never actually *define* it anywhere. – Some programmer dude Oct 26 '15 at 12:51
  • @JoachimPileborg I don't understand what you mean by define? Didn't I define it in main.h or am I thinking of something else? – Happy Crayfish Oct 26 '15 at 12:57
  • In the header file you *declare* the static member variable `isRunning`, you need to *define* as well, which you do in a separate source file. Do some searching and you will find many examples. – Some programmer dude Oct 26 '15 at 12:59
  • @JoachimPileborg So, I think I fixed the erros by playing around, though I'm not 100% sure. i'll play around some more. – Happy Crayfish Oct 26 '15 at 13:03
  • @JoachimPileborg I have 2 files, none of them include anything and still I get the same linking error... – Happy Crayfish Oct 26 '15 at 13:19
  • On one source file, outside of any function (i.e. in the global scope) add `bool main::isRunning;` That's the *definition* needed. – Some programmer dude Oct 26 '15 at 13:21
  • @JoachimPileborg I did, the error was removed however the window wouldn't display, Restarting fresh again. 2 classes : Core, Window. Core, includes Window.h and creates an object. And I get the same linking error as before. – Happy Crayfish Oct 26 '15 at 13:24

0 Answers0