Author Topic: I'm stumped with glew32, could someone point me in the right direction?  (Read 7231 times)

Offline sramos58

  • Single posting newcomer
  • *
  • Posts: 3
I'm working with openglbook.com on chapter 2. i have glew32, freeglut and opengl32 in my project linkers (and have tried putting those 3 solely in my compiler linker without being in the project linker at the same time) and in that order.

exactly what i have in my main.cpp. which is the 1 and only file in my C::B project

Code
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#define WINDOW_TITLE_PREFIX "Chapter 2"
#define GLEW_STATIC

int CurrentWidth = 800,
CurrentHeight = 600,
WindowHandle = 0;

unsigned FrameCount = 0;
GLuint
    VertexShaderId,
    FragmentShaderId,
    ProgramId,
    VaoId,
    VboId,
    ColorBufferId;

const GLchar* VertexShader =
{
    "#version 400\n"\
    "layout(location=0) in vec4 in_Position;\n"\
    "layout(location=1) in vec4 in_Color;\n"\
    "out vec4 ex_Color;\n"\

    "void main(void)\n"\
    "{\n"\
    "   gl_Position = in_Position;\n"\
    "   ex_Color = in_Color;\n"\
    "}\n"
};

const GLchar* FragmentShader =
{
    "#version 400\n"\

    "in vec4 ex_Color;\n"\
    "out vec4 out_Color;\n"\

    "void main(void)\n"\
    "{\n"\
    "   out_Color = ex_Color;\n"\
    "}\n"
};

void Initialize(int, char*[]);
void InitWindow(int argc, char* argv[]);
void ResizeFunction(int, int);
void RenderFunction(void);
void TimerFunction(int);
void IdleFunction(void);
//---------------------------------------------------------------------------
void Cleanup(void);
void CreateVBO(void);
void DestroyVBO(void);
void CreateShaders(void);
void DestroyShaders(void);

int main(int argc, char* argv[])
{
Initialize(argc, argv);

glutMainLoop();

exit(EXIT_SUCCESS);
}

void Initialize(int argc, char* argv[])
{
GLenum GlewInitResult;

InitWindow(argc, argv);

    GlewInitResult = glewInit();
if (GLEW_OK != GlewInitResult) {
fprintf(
stderr,
"ERROR: %s\n",
glewGetErrorString(GlewInitResult)
);
exit(EXIT_FAILURE);
}

fprintf(
stdout,
"INFO: OpenGL Version: %s\n",
glGetString(GL_VERSION)
);

    CreateShaders();
    CreateVBO();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

void InitWindow(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitContextVersion(4, 0);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);

glutInitWindowSize(CurrentWidth, CurrentHeight);

glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);

if(WindowHandle < 1) {
fprintf(
stderr,
"ERROR: Could not create a new rendering window.\n"
);
exit(EXIT_FAILURE);
}

glutReshapeFunc(ResizeFunction);
glutDisplayFunc(RenderFunction);
glutIdleFunc(IdleFunction);
    glutTimerFunc(0, TimerFunction, 0);
    glutCloseFunc(Cleanup);

}

void ResizeFunction(int Width, int Height)
{
CurrentWidth = Width;
CurrentHeight = Height;
glViewport(0, 0, CurrentWidth, CurrentHeight);
}

void RenderFunction(void)
{
    ++FrameCount;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);

glutSwapBuffers();
glutPostRedisplay();
}

void IdleFunction(void)
{
    glutPostRedisplay();
}

void TimerFunction(int Value)
{
    if (0 != Value) {
        char* TempString = (char*)
            malloc(512 + strlen(WINDOW_TITLE_PREFIX));

        sprintf(
            TempString,
            "%s: %d Frames Per Second @ %d x %d",
            WINDOW_TITLE_PREFIX,
            FrameCount * 10,
            CurrentWidth,
            CurrentHeight
        );

        glutSetWindowTitle(TempString);
        free(TempString);
    }

    FrameCount = 0;
    glutTimerFunc(100, TimerFunction, 1);
}

void Cleanup(void)
{
    DestroyShaders();
    DestroyVBO();
}

void CreateVBO(void)
{
    GLfloat Vertices[] = {
        -0.8f, -0.8f, 0.0f, 1.0f,
         0.0f,  0.8f, 0.0f, 1.0f,
         0.8f, -0.8f, 0.0f, 1.0f
    };

    GLfloat Colors[] = {
        1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 1.0f, 0.0f, 1.0f,
        0.0f, 0.0f, 1.0f, 1.0f
    };

    GLenum ErrorCheckValue = glGetError();

    glGenVertexArrays(1, &VaoId);
    glBindVertexArray(VaoId);

    glGenBuffers(1, &VboId);
    glBindBuffer(GL_ARRAY_BUFFER, VboId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);

    glGenBuffers(1, &ColorBufferId);
    glBindBuffer(GL_ARRAY_BUFFER, ColorBufferId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);

    ErrorCheckValue = glGetError();
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
            stderr,
            "ERROR: Could not create a VBO: %s \n",
            gluErrorString(ErrorCheckValue)
        );

        exit(-1);
    }
 }

 void DestroyVBO(void)
{
    GLenum ErrorCheckValue = glGetError();

    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glDeleteBuffers(1, &ColorBufferId);
    glDeleteBuffers(1, &VboId);

    glBindVertexArray(0);
    glDeleteVertexArrays(1, &VaoId);

    ErrorCheckValue = glGetError();
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
            stderr,
            "ERROR: Could not destroy the VBO: %s \n",
            gluErrorString(ErrorCheckValue)
        );

        exit(-1);
    }
}

void CreateShaders(void)
{
    GLenum ErrorCheckValue = glGetError();

    VertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(VertexShaderId, 1, &VertexShader, NULL);
    glCompileShader(VertexShaderId);

    FragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(FragmentShaderId, 1, &FragmentShader, NULL);
    glCompileShader(FragmentShaderId);

    ProgramId = glCreateProgram();
        glAttachShader(ProgramId, VertexShaderId);
        glAttachShader(ProgramId, FragmentShaderId);
    glLinkProgram(ProgramId);
    glUseProgram(ProgramId);

    ErrorCheckValue = glGetError();
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
            stderr,
            "ERROR: Could not create the shaders: %s \n",
            gluErrorString(ErrorCheckValue)
        );

        exit(-1);
    }
}

void DestroyShaders(void)
{
    GLenum ErrorCheckValue = glGetError();

    glUseProgram(0);

    glDetachShader(ProgramId, VertexShaderId);
    glDetachShader(ProgramId, FragmentShaderId);

    glDeleteShader(FragmentShaderId);
    glDeleteShader(VertexShaderId);

    glDeleteProgram(ProgramId);

    ErrorCheckValue = glGetError();
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
            stderr,
            "ERROR: Could not destroy the shaders: %s \n",
            gluErrorString(ErrorCheckValue)
        );

        exit(-1);
    }
}

I've already made sure I'm using the 32bit freeglut and glew. And I have all libs, dlls and headers in the respective mingw folders. Also freeglut.dll and glew32.dll and the libs are even in my programs main folder just in case.

Here's the build log.

Code
------------ Clean: Debug in 40 (compiler: GNU GCC Compiler)---------------

Cleaned "40 - Debug"

-------------- Build: Debug in 40 (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall  -g    -ID:\backup\funstuff\programming\freeglut\include  -c D:\backup\funstuff\programming\40\main.cpp -o obj\Debug\main.o
mingw32-g++.exe  -o bin\Debug\40.exe obj\Debug\main.o    -lglew32 -lfreeglut -lopengl32

But the remaining lines of the build log are riddled with these errors.
Code
obj\Debug\main.o: In function `Z9CreateVBOv':
D:/backup/funstuff/programming/40/main.cpp:196: undefined reference to `_imp____glewGenVertexArrays'
D:/backup/funstuff/programming/40/main.cpp:197: undefined reference to `_imp____glewBindVertexArray'
D:/backup/funstuff/programming/40/main.cpp:199: undefined reference to `_imp____glewGenBuffers'
D:/backup/funstuff/programming/40/main.cpp:200: undefined reference to `_imp____glewBindBuffer'
D:/backup/funstuff/programming/40/main.cpp:201: undefined reference to `_imp____glewBufferData'
D:/backup/funstuff/programming/40/main.cpp:202: undefined reference to `_imp____glewVertexAttribPointer'
D:/backup/funstuff/programming/40/main.cpp:203: undefined reference to `_imp____glewEnableVertexAttribArray'
D:/backup/funstuff/programming/40/main.cpp:205: undefined reference to `_imp____glewGenBuffers'
D:/backup/funstuff/programming/40/main.cpp:206: undefined reference to `_imp____glewBindBuffer'
D:/backup/funstuff/programming/40/main.cpp:207: undefined reference to `_imp____glewBufferData'
D:/backup/funstuff/programming/40/main.cpp:208: undefined reference to `_imp____glewVertexAttribPointer'
D:/backup/funstuff/programming/40/main.cpp:209: undefined reference to `_imp____glewEnableVertexAttribArray'
D:/backup/funstuff/programming/40/main.cpp:218: undefined reference to `gluErrorString@4'
obj\Debug\main.o: In function `Z10DestroyVBOv':
D:/backup/funstuff/programming/40/main.cpp:228: undefined reference to `_imp____glewDisableVertexAttribArray'
D:/backup/funstuff/programming/40/main.cpp:229: undefined reference to `_imp____glewDisableVertexAttribArray'
D:/backup/funstuff/programming/40/main.cpp:231: undefined reference to `_imp____glewBindBuffer'
D:/backup/funstuff/programming/40/main.cpp:233: undefined reference to `_imp____glewDeleteBuffers'
D:/backup/funstuff/programming/40/main.cpp:234: undefined reference to `_imp____glewDeleteBuffers'
D:/backup/funstuff/programming/40/main.cpp:236: undefined reference to `_imp____glewBindVertexArray'
D:/backup/funstuff/programming/40/main.cpp:237: undefined reference to `_imp____glewDeleteVertexArrays'
D:/backup/funstuff/programming/40/main.cpp:246: undefined reference to `gluErrorString@4'
obj\Debug\main.o: In function `Z13CreateShadersv':
D:/backup/funstuff/programming/40/main.cpp:256: undefined reference to `_imp____glewCreateShader'
D:/backup/funstuff/programming/40/main.cpp:257: undefined reference to `_imp____glewShaderSource'
D:/backup/funstuff/programming/40/main.cpp:258: undefined reference to `_imp____glewCompileShader'
D:/backup/funstuff/programming/40/main.cpp:260: undefined reference to `_imp____glewCreateShader'
D:/backup/funstuff/programming/40/main.cpp:261: undefined reference to `_imp____glewShaderSource'
D:/backup/funstuff/programming/40/main.cpp:262: undefined reference to `_imp____glewCompileShader'
D:/backup/funstuff/programming/40/main.cpp:264: undefined reference to `_imp____glewCreateProgram'
D:/backup/funstuff/programming/40/main.cpp:265: undefined reference to `_imp____glewAttachShader'
D:/backup/funstuff/programming/40/main.cpp:266: undefined reference to `_imp____glewAttachShader'
D:/backup/funstuff/programming/40/main.cpp:267: undefined reference to `_imp____glewLinkProgram'
D:/backup/funstuff/programming/40/main.cpp:268: undefined reference to `_imp____glewUseProgram'
D:/backup/funstuff/programming/40/main.cpp:277: undefined reference to `gluErrorString@4'
obj\Debug\main.o: In function `Z14DestroyShadersv':
D:/backup/funstuff/programming/40/main.cpp:287: undefined reference to `_imp____glewUseProgram'
D:/backup/funstuff/programming/40/main.cpp:289: undefined reference to `_imp____glewDetachShader'
D:/backup/funstuff/programming/40/main.cpp:290: undefined reference to `_imp____glewDetachShader'
D:/backup/funstuff/programming/40/main.cpp:292: undefined reference to `_imp____glewDeleteShader'
D:/backup/funstuff/programming/40/main.cpp:293: undefined reference to `_imp____glewDeleteShader'
D:/backup/funstuff/programming/40/main.cpp:295: undefined reference to `_imp____glewDeleteProgram'
D:/backup/funstuff/programming/40/main.cpp:304: undefined reference to `gluErrorString@4'
collect2.exe: error: ld returned 1 exit status

I just installed C::B on a fresh windows 7 x64 reinstall a few days ago, and installed freeglut and glew32 soon after.

I'm extremely green when it comes to all this and barely understand the jargon.

But from what I understand so far, I have the 3 appropriate libraries linked in the project folder, and all the headers in proper order (I even copied the source code file from the website and pasted it to find the same issues).

I remember from an earlier problem I figured out that undefined references to items with "_imp__" in front of it strictly means it's dll/lib related. But I'm burnt.

What am I doing wrong?
« Last Edit: June 21, 2013, 03:31:48 am by sramos58 »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
This is NOT a valid topic for the CB Website.
Please read the rules!
http://forums.codeblocks.org/index.php/topic,9996.0.html

Quote
2. Compiler/Linker errors are NOT Code::Blocks errors. Usually, C++ newcomers tend to confuse the Editor/IDE (Code::Blocks) with the Compiler (MINGW / GCC). You may see some errors in the compiler output because you missed to do something right in your code. But that's not Code::Blocks troubleshooting, that's C++ troubleshooting and does not belong in here. If your program doesn't compile, READ THE C++ MANUAL.

3. Is your problem library, framework specific? There are appropriate forums for it, not the Code::Blocks forum.


Note: The problem is likely you did NOT place this line before you included the headers like "glew.h"

Code
#define GLEW_STATIC

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline sramos58

  • Single posting newcomer
  • *
  • Posts: 3
I did place that #define before glew.h. no difference

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
As Tim posted, you are violating our forum rules:
This is NOT a valid topic for the CB Website.
Please read the rules!
http://forums.codeblocks.org/index.php/topic,9996.0.html

Quote
2. Compiler/Linker errors are NOT Code::Blocks errors. Usually, C++ newcomers tend to confuse the Editor/IDE (Code::Blocks) with the Compiler (MINGW / GCC). You may see some errors in the compiler output because you missed to do something right in your code. But that's not Code::Blocks troubleshooting, that's C++ troubleshooting and does not belong in here. If your program doesn't compile, READ THE C++ MANUAL.

3. Is your problem library, framework specific? There are appropriate forums for it, not the Code::Blocks forum.

Topic locked !