链接问题 C++、glew 和 SDL

Posted

技术标签:

【中文标题】链接问题 C++、glew 和 SDL【英文标题】:Linking problems C++, glew, and SDL 【发布时间】:2015-12-20 12:12:06 【问题描述】:

我遇到了一些链接器错误,我无法真正理解它们的含义,所以我很感激一些帮助。我正在尝试编写一个简单的程序来创建一个基本的片段着色器和一个基本的顶点着色器。这是代码:

Shader.h~

#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <glew.h>


class Shader

    public:
    const static unsigned int NUMOFSHADERS = 2;

    GLuint m_program;
    GLuint m_shaders[NUMOFSHADERS];

    void Bind();
    static std::string LoadShader(const std::string& filename);
    static GLuint CreateShader(const std::string& text, GLenum shadertype);
    Shader(const std::string& filename);
    virtual ~Shader();
;

Shader.cpp~

#include "Shader.h"


Shader::Shader(const std::string& filename)

    m_program = glCreateProgram();
    m_shaders[0] = CreateShader(LoadShader(filename + ".shade_v"),     GL_VERTEX_SHADER);
    m_shaders[1] = CreateShader(LoadShader(filename + ".shade_f"),     GL_FRAGMENT_SHADER);

    for (unsigned int i = 0; NUMOFSHADERS; i++)
    
        glAttachShader(m_program, m_shaders[i]);
    ;

    glBindAttribLocation(m_program, 0, "position");
    glLinkProgram(m_program);


Shader::~Shader()

    for (unsigned int i = 0; NUMOFSHADERS; i++)
    
        glDetachShader(m_program, m_shaders[i]);
        glDeleteShader(m_shaders[i]);
    

    glDeleteProgram(m_program);


static std::string LoadShader(const std::string& filename)


    std::ifstream file;
    std::string output;
    std::string line;
    file.open(filename.c_str());
    if (file.is_open())
    
        while (file)
        
            std::getline(file, line);
            output.append(line + "\n");
        
        file.close();
        return output;
    


static GLuint CreateShader(const std::string& text, GLenum shadertype)

    GLuint shader = glCreateShader(shadertype);
    const GLchar* shadersourcestrings[1];
    GLint shadersourcelengths[1];

    shadersourcestrings[0] = text.c_str();
    shadersourcelengths[0] = text.length();

    glShaderSource(shader, 1, shadersourcestrings, shadersourcelengths);
    glCompileShader(shader);

    return shader;


void Shader::Bind()

    glUseProgram(m_program);

Display.h~

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

class Display

public:
    void Update();
    void Clear(float r, float g, float b, float a);
    bool isClosed();
    Display(int width, int height, const char *title);
    virtual  ~Display();
private:
    bool m_isClosed;
    SDL_Window* m_window;
    SDL_GLContext m_context;
;

Display.cpp~

#pragma once
#include "Display.h"

Display::Display(int width, int height, const char *title)

    if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
    
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

        m_window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,     SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
        m_context = SDL_GL_CreateContext(m_window);

        GLenum status = glewInit();
        if (status != GLEW_OK)
            std::cerr << "this is where the problem is" << std::endl;
        
        m_isClosed = false;
    



Display::~Display()

        std::cerr << "destructor ran" << std::endl;
        SDL_GL_DeleteContext(m_context);
        SDL_DestroyWindow(m_window);
        SDL_Quit();


void Display::Update()

    SDL_Event event;
    SDL_GL_SwapWindow(m_window);

    while (SDL_PollEvent(&event))
    
        switch (event.type)
        
        case SDL_QUIT:
            m_isClosed = true;
        
    


void Display::Clear(float r, float g, float b, float a)

    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT);


bool Display::isClosed()
    return m_isClosed;

ma​​in.cpp~

#include <iostream>
#include "Display.h"
#include "Shader.h"

int main(int argc, char *argv[])

    Display display(800, 600, "test");
    Shader shader("basicshader");

    while (!display.isClosed())
    
        display.Clear(1.0f, 0.0f, 0.0f, 1.0f);
        shader.Bind();
        display.Update();
    

    return 0;

这是我收到的错误

error LNK2019: unresolved external symbol "public: static unsigned int __cdecl Shader::CreateShader(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,unsigned int)" (?CreateShader@Shader@@SAIABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@I@Z) referenced in function "public: __thiscall Shader::Shader(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Shader@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)   C:\Users\Nas\documents\visual studio 2013\Projects\Open GL\Open GL\Shader.obj

error LNK2019: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl Shader::LoadShader(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?LoadShader@Shader@@SA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@@Z) referenced in function "public: __thiscall Shader::Shader(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Shader@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) C:\Users\Nas\documents\visual studio 2013\Projects\Open GL\Open GL\Shader.obj

error LNK1120: 2 unresolved externals   C:\Users\Nas\documents\visual studio 2013\Projects\Open GL\Debug\Open GL.exe    1

【问题讨论】:

你付出了一切,除了错误的东西。你给出什么命令来链接这个? 我不知道你到底在问我什么。 【参考方案1】:

您在CreateShader 实现之前缺少Shader::。此外,您必须在实现中删除 static 关键字:

         \/
GLuint Shader::CreateShader(const std::string& text, GLenum shadertype)

    ...

您现在定义的是全局范围内的局部静态方法::CreateShader,它与成员函数Shader::CreateShader 完全无关。

【讨论】:

如果我在这里错了,请纠正我,但我很确定当你将任何类成员定义为静态时,不需要“Shader::”,如果没有,那么我必须做其他事情错了,因为我试过了,它并没有解决我的问题,它只是给出了更多的错误:P。 抱歉,我监督了一些事情。我会更新我的答案。类成员(静态或非静态)始终必须正确实现。并且 static 关键字必须只出现在声明中,而不是实现中。您现在执行此操作的方式在全局范围内定义并实现了一个与成员函数完全无关的新静态方法。

以上是关于链接问题 C++、glew 和 SDL的主要内容,如果未能解决你的问题,请参考以下文章

glew Missing GL version c++ using SDL2 2.0.5 and glew 2.0.0

创建网格和启动窗口 SDL/GLEW C++ 时访问冲突

OpenGL 深度测试不工作 (GLEW/SDL2)

GLEW + cmake 链接失败“未定义对符号 glDrawElements 的引用”+“命令行中缺少 DSO”

c++链接器错误LNK2005已经在SDL中定义

无法将 Glew 与 Cmake 链接