1

I'm trying to compile a simple SDL example from a book and I'm getting these errors. I'm near positive everything is linked correctly, because I was able to get other SDL examples to compile fine. I'm using Visual Studio 2013.

This code was intended for use with SDL 2.0 and links to SDL.lib, while I'm using SDL 2.0.3 and linking to SDL2.lib. However, I don't see any reason why that affect anything here.

Game.cpp

#include "Game.h"
#include <iostream>

Game::Game()
{

}

bool Game::init(const char* title, int xpos, int ypos, int width,
    int height, int flags)
{
    // attempt to initialize SDL
    if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {
        std::cout << "SDL init success\n";
        // init the window
        m_pWindow = SDL_CreateWindow(title, xpos, ypos,
            width, height, flags);
        if (m_pWindow != 0) // window init success
        {
            std::cout << "window creation success\n";
            m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
            if (m_pRenderer != 0) // renderer init success
            {
                std::cout << "renderer creation success\n";
                SDL_SetRenderDrawColor(m_pRenderer,
                    255, 255, 255, 255);
            }
            else
            {
                std::cout << "renderer init fail\n";
                return false; // renderer init fail
            }
        }
        else
        {
            std::cout << "window init fail\n";
            return false; // window init fail
        }
    }
    else
    {
        std::cout << "SDL init fail\n";
        return false; // SDL init fail
    }
    std::cout << "init success\n";
    m_bRunning = true; // everything inited successfully,start the main loop
    return true;
}

void Game::render()
{
    SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color
    SDL_RenderPresent(m_pRenderer); // draw to the screen
}

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

void Game::clean()
{
    std::cout << "cleaning game\n";
    SDL_DestroyWindow(m_pWindow);
    SDL_DestroyRenderer(m_pRenderer);
    SDL_Quit();
}

Game.h

 #ifndef __Game__
    #define __Game__
    #include "SDL.h"
    class Game
    {
    public:
        Game();
        ~Game();
        bool init(const char* title, int xpos, int ypos, int width, int
            height, int flags);
        void render();
        void update();
        void handleEvents();
        void clean();
        bool running() { return m_bRunning; }
    private:
        SDL_Window* m_pWindow;
        SDL_Renderer* m_pRenderer;
        bool m_bRunning;
    };
    #endif /* defined(__Game__) */

Main.cpp

#include "Game.h"
// our Game object
Game* g_game = 0;
int main(int argc, char* argv[])
{
    g_game = new Game();
    g_game->init("Chapter 1", 100, 100, 640, 480, 0);
    while (g_game->running())
    {
        g_game->handleEvents();
        g_game->update();
        g_game->render();
    }
    g_game->clean();
    return 0;
}

And the following are the errors/warnings I recieve:

Error   3   error LNK1120: 1 unresolved externals   c:\users\alexn\documents\visual studio 2013\Projects\SDL_template2\Debug\SDL_template2.exe  SDL_template2

Error   2   error LNK2019: unresolved external symbol "public: void __thiscall Game::update(void)" (?update@Game@@QAEXXZ) referenced in function _SDL_main  c:\Users\alexn\documents\visual studio 2013\Projects\SDL_template2\SDL_template2\Main.obj   SDL_template2

Warning 1   warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library    c:\Users\alexn\documents\visual studio 2013\Projects\SDL_template2\SDL_template2\MSVCRTD.lib(cinitexe.obj)  SDL_template2
1
  • 1
    Error 2 is simple enough, there's no definition of Game::update().
    – user657267
    Jan 21, 2015 at 4:12

1 Answer 1

1

You're calling Game::update() from your main(), however it seems that you didn't actually implement it.

As for the warning, refer to this question for more info. It's likely that you're trying to link with a version of SDL (or some other lib) that was built with a different C runtime.

1
  • It turns out adding an empty update method was all it needed to work, thank you.
    – aredscout
    Jan 21, 2015 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.