-2

Whenever I try to compile and run my C++ SDL/Glew game, I get the linker error: LNK2019. I've looked into it in the MSDN and that says that it's C code in a C++ file not wrapped in an extern 'C' { ... }.

I'm using Visual Studio 2015 Community and the latest version of Glew, on Windows 8.1.

Here is the Error I keep getting:

LNK2019: unresolved external symbol _imp_glClear@4 referenced in function "private void _thiscall MainGame::draw(void)" (?draw@MainGame@2AAeXXZ)

LNK2019: unresolved external symbol _imp_glClearColor@16 referenced in function "private: void _thiscall MainGame::initSystems(void)" (?initSystems@MainGame@@AAEXXZ)

LNK2019: unresolved external symbol _imp_glClearDepth@8 referenced in function "private void _thiscall MainGame::draw(void)" (?draw@MainGame@@AAEXXZ

MainGame.cpp:

#include "MainGame.h"

void fatalError(std::string errorString) {
    std::cout << "FATAL ERROR:" << errorString << std::endl;
    std::cout << "Enter any key to quit...";
    int tmp;
    std::cin >> tmp;
    SDL_Quit();
    exit(1);
}

MainGame::MainGame() {
    window = nullptr;
    width = 960;
    height = 540;
    gameState = GameState::PLAY;
}

MainGame::~MainGame() {
}

void MainGame::run() {
    initSystems();
    gameLoop();
}

void MainGame::initSystems() {
    SDL_Init(SDL_INIT_EVERYTHING);
    window = SDL_CreateWindow("Acorn", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);

    if (window == nullptr) {
        fatalError("Failed to Initialize the Window");
    }

    SDL_GLContext glContext = SDL_GL_CreateContext(window);
    if (glContext == nullptr) {
        fatalError("Failed to Initialize OpenGL");
    }
    GLenum error = glewInit();

    if (error != GLEW_OK) {
        fatalError("Failed to Initialize Glew");
    }

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

void MainGame::processInput() {
    SDL_Event sdlevent;
    while (SDL_PollEvent(&sdlevent)) {
        switch (sdlevent.type) {
        case SDL_QUIT:
            gameState = GameState::QUIT;
            break;
        }
    }
}

void MainGame::draw() {
    glClearDepth(1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glClear(GL_DEPTH_BUFFER_BIT);

    SDL_GL_SwapWindow(window);
}

void MainGame::gameLoop() {
    while (gameState != GameState::QUIT) {
        draw();
        processInput();
    }
}

MainGame.h:

#pragma once
#include <GL/glew.h>
#include <SDL/SDL.h>
#include <iostream>
#include <string>

enum class GameState {
    PLAY,
    QUIT
};

class MainGame  {
public:
    MainGame();
    ~MainGame();
    void run();
private:
    GameState gameState;
    int width, height;
    SDL_Window* window;

    void initSystems();
    void gameLoop();
    void processInput();
    void draw();
};
Marco A.
  • 43,032
  • 26
  • 132
  • 246
Duphus
  • 191
  • 1
  • 10
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – genpfault Sep 08 '15 at 13:21

2 Answers2

4

The problem doesn't look like related to glew or SDL, the functions that the linker is unable to find are part of the opengl library.

Are you correctly linking with opengl? You need to add opengl32.lib to Additional Dependencies of Linker as explained here.

Jack
  • 131,802
  • 30
  • 241
  • 343
0

I used to have similar problems with glew, but the solution was to compile glew.c yourself, and link with resulting glew.o. This involved few lines of makefiles etc, but since you're using different toolchain, you need to do something different.

tp1
  • 1,197
  • 10
  • 17