PDA

View Full Version : Problems when linking with GLEW(MinGW32)



avleze
11-28-2014, 08:17 PM
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:

||=== Build: Debug in Exercise 4 (compiler: GNU GCC Compiler) ===|


obj\Debug\Shader.o||In function `ZN6Shader3UseEv':|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|45|u ndefined reference to `_imp____glewUseProgram'|

obj\Debug\Shader.o||In function `ZN6Shader14CompileShadersEPKcS1_':|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|56|u ndefined reference to `_imp____glewCreateShader'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|57|u ndefined reference to `_imp____glewShaderSource'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|58|u ndefined reference to `_imp____glewCompileShader'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|61|u ndefined reference to `_imp____glewGetShaderiv'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|65|u ndefined reference to `_imp____glewGetShaderInfoLog'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|69|u ndefined reference to `_imp____glewCreateShader'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|70|u ndefined reference to `_imp____glewShaderSource'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|71|u ndefined reference to `_imp____glewCompileShader'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|74|u ndefined reference to `_imp____glewGetShaderiv'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|78|u ndefined reference to `_imp____glewGetShaderInfoLog'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|84|u ndefined reference to `_imp____glewCreateProgram'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|85|u ndefined reference to `_imp____glewAttachShader'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|86|u ndefined reference to `_imp____glewAttachShader'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|87|u ndefined reference to `_imp____glewLinkProgram'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|89|u ndefined reference to `_imp____glewGetProgramiv'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|93|u ndefined reference to `_imp____glewGetShaderInfoLog'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|97|u ndefined reference to `_imp____glewDeleteShader'|
C:\Users\avleze\Desktop\OpenGLEjer\Shader.cpp|98|u ndefined reference to `_imp____glewDeleteShader'|
||=== Build failed: 19 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


The files:

main.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
#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

#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.

thor36
12-01-2014, 08:25 AM
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.

Alexey Boreskov
12-01-2014, 08:32 AM
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.

avleze
12-01-2014, 03:47 PM
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.

thor36
12-02-2014, 11:46 AM
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 :)

avleze
12-03-2014, 05:52 PM
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.