PDA

View Full Version : Link Opengl in a Dll project



JohnnyKPL
06-01-2015, 12:31 PM
I'm trying to create my first 2d game engine. It would be a set of libraries that give me more efficiency and handiness to develop 2d games. Unfortunately it gives me linking error while I compile.I check if I've linked fine the libraries and, why not, if the libraries exists but I can't find the solution.
Here's the errors (it's in italian, sorry):


# Erorre = error
# simbolo esterno = external symbol
# non risolto = unresolved

Errore 1 error LNK2001: simbolo esterno ___glewAttachShader non risolto C:\Users\Lorenzo\Documents\Visual Studio 2013\Projects\Engine\EngineDll\Graphics.obj EngineDll
Errore 2 error LNK2001: simbolo esterno ___glewCompileShader non risolto C:\Users\Lorenzo\Documents\Visual Studio 2013\Projects\Engine\EngineDll\Graphics.obj EngineDll
Errore 3 error LNK2001: simbolo esterno ___glewCreateProgram non risolto C:\Users\Lorenzo\Documents\Visual Studio 2013\Projects\Engine\EngineDll\Graphics.obj EngineDll
Errore 4 error LNK2001: simbolo esterno ___glewCreateShader non risolto C:\Users\Lorenzo\Documents\Visual Studio 2013\Projects\Engine\EngineDll\Graphics.obj EngineDll
Errore 5 error LNK2001: simbolo esterno ___glewDeleteProgram non risolto C:\Users\Lorenzo\Documents\Visual Studio 2013\Projects\Engine\EngineDll\Graphics.obj EngineDll
Errore 6 error LNK2001: simbolo esterno ___glewDeleteShader non risolto C:\Users\Lorenzo\Documents\Visual Studio 2013\Projects\Engine\EngineDll\Graphics.obj EngineDll
Errore 7 error LNK2001: simbolo esterno ___glewDetachShader non risolto C:\Users\Lorenzo\Documents\Visual Studio 2013\Projects\Engine\EngineDll\Graphics.obj EngineDll
Errore 8 error LNK2001: simbolo esterno ___glewLinkProgram non risolto C:\Users\Lorenzo\Documents\Visual Studio 2013\Projects\Engine\EngineDll\Graphics.obj EngineDll
Errore 9 error LNK2001: simbolo esterno ___glewShaderSource non risolto C:\Users\Lorenzo\Documents\Visual Studio 2013\Projects\Engine\EngineDll\Graphics.obj EngineDll
Errore 10 error LNK2001: simbolo esterno ___glewUseProgram non risolto C:\Users\Lorenzo\Documents\Visual Studio 2013\Projects\Engine\EngineDll\Graphics.obj EngineDll


And the header file that I'm trying to compile:


#include <GL\glew.h>
#include <glm\glm.hpp>

#define DLLEXPORT __declspec(dllexport)
#define DLLIMPORT __declspec(dllimport)

namespace gr
{
struct Color
{
private:
glm::vec3 color;

public:
Color(glm::vec3 color) : color(color) {}
~Color() {}

DLLEXPORT GLfloat operator[](unsigned char i) { return color[i]; }
};

struct Shader
{
private:
GLuint program, shaders[2];

public:
Shader();
~Shader();

void use();
};

class Drawable
{
protected:
GLfloat x, y, z;

public:
Drawable(GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) {}
~Drawable() {}

virtual void Render() = NULL;
};

class Rectangle : public Drawable
{
private:
GLfloat width, height;
Color color;

public:
Rectangle(GLfloat x, GLfloat y, GLfloat z, GLfloat width, GLfloat height, Color color) : Drawable(x, y, z), width(width), height(height), color(color) {}
~Rectangle() {}

void Render();
};
}


P.S: My project is for Windows x64 and I don't use precompiled headers!

JohnnyKPL
06-03-2015, 06:43 AM
Anyone know how can I resolve?

JohnnyKPL
06-03-2015, 11:32 AM
Maybe, I have to init all function with __declspec(dllimport)?

Something like:

__declspec(dllimport) GLuint glCreateShader(GLenum);

/// call shader function

Here's the cpp where I called OpenGL functions:


#include "Graphics.hpp"

static const GLchar *shaderSources[2] =
{
{
"void main()"
"{"
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
"}"
},
{
"void main()"
"{"
"gl_FragColor = gl_Color;"
"}"
}
};

static GLuint createShader(const GLchar *source, GLenum type)
{
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);

return shader;
}

gr::Shader::Shader()
{
program = glCreateProgram();

for (unsigned short i = 0; i < 2; i++)
{
shaders[i] = createShader(shaderSources[i], 0x4B31 - i);
glAttachShader(program, shaders[i]);
}

glLinkProgram(program);
}

gr::Shader::~Shader()
{
for (unsigned short i = 0; i < 2; i++)
{
glDetachShader(program, shaders[i]);
glDeleteShader(shaders[i]);
}

glDeleteProgram(program);
}

void gr::Shader::use()
{
glUseProgram(program);
}