Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Problems when linking with GLEW(MinGW32)

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2014
    Posts
    3

    Problems when linking with GLEW(MinGW32)

    Hello, i want to build a program with MinGW32 which have three files, one called main.cpp (where glewInit() is called), other called Shader.cpp and their header Shader.hpp . Well, the problem is that when I try to build the entire program I get undefined references in the Shader.cpp file. However, if I remove both Shader.cpp and Shader.hpp the program is builted and i dont get problems. I have the libraries correctly configurated in codeblocks and these libraries are built for MinGW, so i dont know what happening.

    The problems are:
    Code :
    ||=== Build: Debug in Exercise 4 (compiler: GNU GCC Compiler) ===|
     
     
    obj\Debug\Shader.o||In function `ZN6Shader3UseEv':|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|45|undefined reference to `_imp____glewUseProgram'|
     
    obj\Debug\Shader.o||In function `ZN6Shader14CompileShadersEPKcS1_':|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|56|undefined reference to `_imp____glewCreateShader'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|57|undefined reference to `_imp____glewShaderSource'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|58|undefined reference to `_imp____glewCompileShader'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|61|undefined reference to `_imp____glewGetShaderiv'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|65|undefined reference to `_imp____glewGetShaderInfoLog'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|69|undefined reference to `_imp____glewCreateShader'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|70|undefined reference to `_imp____glewShaderSource'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|71|undefined reference to `_imp____glewCompileShader'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|74|undefined reference to `_imp____glewGetShaderiv'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|78|undefined reference to `_imp____glewGetShaderInfoLog'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|84|undefined reference to `_imp____glewCreateProgram'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|85|undefined reference to `_imp____glewAttachShader'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|86|undefined reference to `_imp____glewAttachShader'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|87|undefined reference to `_imp____glewLinkProgram'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|89|undefined reference to `_imp____glewGetProgramiv'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|93|undefined reference to `_imp____glewGetShaderInfoLog'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|97|undefined reference to `_imp____glewDeleteShader'|
    C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|98|undefined reference to `_imp____glewDeleteShader'|
    ||=== Build failed: 19 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

    The files:

    main.cpp
    Code cpp:
    #include <iostream>
    using namespace std;
    // GLEW
    #define GLEW_STATIC
    #include <GL/glew.h>
    #include "Shader.h"
    // GLFW
    #include <GLFW/glfw3.h>
     
     
    const GLchar* vertexShaderSource = "#version 330 core\n"
        "layout (location = 0) in vec2 position;\n"
        "void main()\n"
        "{\n"
        "gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n"
        "}\0";
    const GLchar* fragmentShaderSource = "#version 330 core\n"
        "out vec4 color;\n"
        "void main()\n"
        "{\n"
            "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
        "}\n\0";
     
     
    void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
     
    int main()
    {
     
        glfwInit();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
     
        GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr); // Windowed
        glfwMakeContextCurrent(window);
     
     
        glfwSetKeyCallback(window, key_callback);
     
     
        glewExperimental = GL_TRUE;
        glewInit();
     
     
        glViewport(0, 0, 800, 600);
     
     
        GLint vertexShader = glCreateShader(GL_VERTEX_SHADER);
        GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        GLint shaderProgram = glCreateProgram();
        glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
        glCompileShader(vertexShader);
        glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
        glCompileShader(fragmentShader);
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glLinkProgram(shaderProgram);
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
     
        GLfloat vertices[] = {
          -0.5f, -0.5f,	// Left (X,Y)
          0.5f, -0.5f, // Right (X,Y)
          0.0f, 0.5f  // Top (X,Y)
        };
        GLuint VBO, VAO;
        glGenVertexArrays(1, &VAO);
        glGenBuffers(1, &VBO);
     
     
     
        glBindVertexArray(VAO);
     
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
     
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
        glEnableVertexAttribArray(0);
     
        glBindVertexArray(0);
     
        // Game loop
        while(!glfwWindowShouldClose(window))
        {
     
            glfwPollEvents();
     
            glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);
     
            glUseProgram(shaderProgram);
            glBindVertexArray(VAO);
            glDrawArrays(GL_TRIANGLES, 0, 3);
            glBindVertexArray(0);
     
            glfwSwapBuffers(window);
        }
     
        glfwTerminate();
        return 0;
    }
     
    void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
    {
        cout << key << endl;
        if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE);
    }
    Shader.hpp
    Code cpp:
    #ifndef SHADER_HPP
    #define SHADER_HPP
     
    #include <string>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <ostream>
    using namespace std;
     
    #include <GL/glew.h>
     
    class Shader
    {
        public:
            GLuint Program;
            Shader(const GLchar* vertexSourcePath, const GLchar* fragmentSourcePath);
            virtual ~Shader();
            void Use();
        protected:
        private:
            void CompileShaders(const GLchar* vSource, const GLchar* fSource);
    };
    #endif // SHADER_HPP
    Code cpp:
    #include "Shader.h"
     
    #include <GL/glew.h>
     
     
    Shader::Shader(const GLchar* vertexSourcePath, const GLchar* fragmentSourcePath)
    {
        string vertexCode;
        string fragmentCode;
        try
        {
            ifstream vShaderFile(vertexSourcePath);
            ifstream fShaderFile(fragmentSourcePath);
     
            stringstream vShaderStream;
            stringstream fShaderStream;
     
            vShaderStream << vShaderFile.rdbuf();
            fShaderStream << fShaderFile.rdbuf();
     
            vShaderFile.close();
            fShaderFile.close();
     
            vertexCode   = vShaderStream.str();
            fragmentCode = fShaderStream.str();
        }
        catch(exception e)
        {
            cout << "ERROR" << endl;
        }
     
        this -> CompileShaders(vertexCode.c_str(), fragmentCode.c_str());
    }
     
    Shader::~Shader()
    {
     
    }
    void Shader::Use()
    {
        glUseProgram(this->Program);
    }
    void Shader::CompileShaders(const GLchar* vSource, const GLchar* fSource)
    {
        GLuint vertex;
        GLuint fragment;
        GLint sucess;
        GLchar infoLog[512];
     
        vertex = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertex, 1, &vSource, nullptr);
        glCompileShader(vertex);
     
        glGetShaderiv(vertex, GL_COMPILE_STATUS, &sucess);
     
        if(!sucess)
        {
            glGetShaderInfoLog(vertex, 512, nullptr, infoLog);
            cout << "ERROR" << endl;
        }
     
        fragment = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragment, 1, &fSource, nullptr);
        glCompileShader(fragment);
     
        glGetShaderiv(fragment, GL_COMPILE_STATUS, &sucess);
     
        if(!sucess)
        {
            glGetShaderInfoLog(fragment, 512, nullptr, infoLog);
            cout << "ERROR" << endl;
        }
     
        this->Program = glCreateProgram();
        glAttachShader(this->Program, vertex);
        glAttachShader(this->Program, fragment);
        glLinkProgram(this->Program);
     
        glGetProgramiv(this->Program, GL_LINK_STATUS, &sucess);
     
        if(!sucess)
        {
            glGetShaderInfoLog(this->Program, 512, nullptr, infoLog);
            cout << "ERROR";
        }
     
        glDeleteShader(vertex);
        glDeleteShader(fragment);
    }

    Thanks.
    Last edited by avleze; 11-29-2014 at 07:51 AM.

  2. #2
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    26
    Have you found your solution yet ? I use CodeBlocks too and when it crashes it removes all the linked libraries from projects, so on next run I have to link them again. I'm not sure if that applies to your problem though, but it's worth a check.

  3. #3
    Newbie Newbie
    Join Date
    Jun 2014
    Posts
    2
    Quote Originally Posted by thor36 View Post
    Have you found your solution yet ? I use CodeBlocks too and when it crashes it removes all the linked libraries from projects, so on next run I have to link them again. I'm not sure if that applies to your problem though, but it's worth a check.
    From the errors it looks like you have not specified GLEW library which you are using.
    Look how you are linking with GLFW and link with GLEW the same way.

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2014
    Posts
    3
    I found a quickly solution, it seems to crash only when GLEW is linked statically.

    When I link it dynamically the problem dissapear. It's a very strangly error.

    Maybe MinGW is linking statically the main.cpp file only and not the rest but i dont know, i would have to do it manually and not with codeblocks support.

    I hope someone can give answers.

    Thanks.
    Last edited by avleze; 12-01-2014 at 02:54 PM.

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    26
    Maybe you could try define GLEW_STATIC in the other file too, like you did in main.cpp . Honestly I'm not sure that's going to solve it, but you can give it a try

  6. #6
    Junior Member Newbie
    Join Date
    Nov 2014
    Posts
    3
    Quote Originally Posted by thor36 View Post
    Maybe you could try define GLEW_STATIC in the other file too, like you did in main.cpp . Honestly I'm not sure that's going to solve it, but you can give it a try
    That was!

    Thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •