C ++ Glew LNK2019错误

时间:2015-09-05 15:42:39

标签: c++ sdl glew

每当我尝试编译并运行我的C ++ SDL / Glew游戏时,我都会收到链接器错误:LNK2019。我在MSDN中查看了它,并说它是C ++文件中的C代码,没有包含在extern'C'{...}中。

我在Windows 8.1上使用Visual Studio 2015社区和最新版本的Glew。

以下是我不断得到的错误:

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();
};

2 个答案:

答案 0 :(得分:4)

问题看起来与glewSDL无关,链接器无法找到的函数是opengl库的一部分。

你正确地与opengl链接?您需要按照here的说明将opengl32.lib添加到链接器其他相关性

答案 1 :(得分:0)

我曾经遇到过与glew类似的问题,但解决方案是自己编译glew.c,并与生成的glew.o链接。这涉及几行makefile等,但由于你使用的是不同的工具链,你需要做一些不同的事情。