2015-12-20 68 views
0

我有一些鏈接器錯誤,我不能真正解讀它們的意思,所以我會感謝一些幫助。我試圖編寫一個簡單的程序,它只是創建一個基本的片段着色器和一個基本的頂點着色器。下面的代碼:鏈接問題C++,glew和SDL

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

的main.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; 
} 

這裏是錯誤我recieving:

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)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) referenced in function "public: __thiscall Shader::Shader(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected]@[email protected]?$basic_[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@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 &)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@Z) referenced in function "public: __thiscall Shader::Shader(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@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 
+0

你給了除了那個錯誤之外的所有東西。你給了什麼命令來鏈接這個? – stark

+0

我不知道你問我到底是什麼。 –

回答

1

你缺少一個Shader::CreateShader實施的前面。此外,您必須刪除static關鍵字在實施:

​​3210

什麼你現在正在定義是在全球範圍內的局部靜態方法::CreateShader,這是完全無關的成員函數Shader::CreateShader

+0

糾正我,如果我在這裏錯了,但我很確定當你將任何類成員定義爲靜態時,不需要「Shader ::」,如果不是,那麼我必須做其他事情,因爲我嘗試了它,沒有解決我的問題,它只是給了更多的錯誤:P。 –

+0

對不起,我監督了一些事情。我會更新我的答案。類成員(靜態或不是)總是必須正確實現。而static關鍵字只能出現在聲明中,而不是在實現中。你現在做的方式現在在全局範圍內定義和實現一個與成員函數完全無關的新靜態方法。 – BDL