LNK2019 - unresolved external symbol - C++ - SDL2 Library

12.1k views Asked by At

I am trying to initialize my game window and can't get past this error. I have tried:

  • double and triple checking all my code for syntax errors
  • checking that the SDL2 library is properly included in my project

Please see below code for your reference:

Header file:

#ifndef Game_hpp
#define Game_hpp
#include "SDL.h"
#include <iostream>

class Game
{
public:
    Game();
    ~Game();

    void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
    void handleEvents();
    void update();
    void render();
    void clean();

    bool running()
    {
        return isRunning;
    }

private:
    bool isRunning;
    SDL_Window *window;
    SDL_Renderer *renderer;
};

#endif /* Game_hpp */

Game.cpp - where functions are defined

 #include "Game.h"

Game::Game()
{}

Game::~Game()
{}

void Game::init(const char *title, int xpos, int ypos, int width, int height, bool fullscreen)
{
    int flags = 0;
    if (fullscreen)
    {
        flags = SDL_WINDOW_FULLSCREEN;
    }

    if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {
        std::cout << "Subsystems Initialised..." << std::endl;

        window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
        if (window)
        {   
            std::cout << "Window Created" << std::endl;
        }

        renderer = SDL_CreateRenderer(window, -1, 0);
        if (renderer)
        {
            SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
            std::cout << "Renderer Created" << std::endl;
        }

        isRunning = true;
    }

    else
    {
        isRunning = false;
    }
}

void Game::handleEvents()
{
    SDL_Event event;
    SDL_PollEvent(&event);
    switch (event.type)
    {
        case SDL_QUIT:
            isRunning = false;
            break;

        default:
            break;
    }
}

void Game::update()
{}

void Game::render()
{
    SDL_RenderClear(renderer);
    //this is where we add stuff to render
    SDL_RenderPresent(renderer);
}

void Game::clean()
{
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();
    std::cout << "Game Cleaned." << std::endl;
}

Main function:

#include "Game.h"

Game *game = nullptr;

int main(int argc, const char * argv[])
{
    game = new Game();

    game->init("BirchEngine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);

    while (game->running())
    {
        game->handleEvents();
        game->update();
        game->render();
     }

    game->clean();

    return 0;
}

The errors I get:

1>SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol SDL_main referenced in function main_utf8

1>C:\Dev\2D_GameEngine\x64\Debug\2D_GameEngine.exe : fatal error LNK1120: 1 unresolved externals

Many thanks for your help!

1

There are 1 answers

4
Benjamin Lindley On BEST ANSWER

SDL (unfortunately, IMO) does this strange thing where they redefine main, in order to abstract away platform specific details of the entry point in applications. In order to not utilize this feature, perform the following. Before including SDL.h, first define SDL_MAIN_HANDLED.

#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>

That inhibits the redefinition of main. Then, in main, before doing anything else with SDL, call this function

SDL_SetMainReady();

That will do any initialization that is needed by SDL (currently does nothing other than setting a global variable named SDL_MainIsReady to true).