Issue compiling OpenGL program that uses GLEW

Hi all,
This is my first post here. I’ve been learning C++, SDL, and OpenGL the last few weeks and finally ran into a problem I can’t seem to get around.
I’m using Code::Blocks in Windows 7.
I was able to set up OpenGL, install freeglut, etc. The sample program that CB gives compiles and works fine.

I then started to go through some tutorials. The first one worked fine (which was opening an OpenGL window), but when I got to the second, where it puts a dot on the screen, it started using glew.h.

Whenever I compile the code they have:


#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "math_3d.h"

GLuint VBO;

static void RenderSceneCB()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_POINTS, 0, 1);

    glDisableVertexAttribArray(0);

    glutSwapBuffers();
}


static void InitializeGlutCallbacks()
{
    glutDisplayFunc(RenderSceneCB);
}

static void CreateVertexBuffer()
{
    Vector3f Vertices[1];
    Vertices[0] = Vector3f(0.0f, 0.0f, 0.0f);

 	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    glutInitWindowSize(1024, 768);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Tutorial 02");

    InitializeGlutCallbacks();

    // Must be done after glut is initialized!
    GLenum res = glewInit();
    if (res != GLEW_OK) {
      fprintf(stderr, "Error: '%s'
", glewGetErrorString(res));
      return 1;
    }

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    CreateVertexBuffer();

    glutMainLoop();

    return 0;
}

I keep getting the following error:


obj\Debug\main.o||In function `RenderSceneCB':|
C:\C Programs\OpenGL Test\main.cpp|33|undefined reference to `_imp____glewEnableVertexAttribArray'|
C:\C Programs\OpenGL Test\main.cpp|34|undefined reference to `_imp____glewBindBuffer'|
C:\C Programs\OpenGL Test\main.cpp|35|undefined reference to `_imp____glewVertexAttribPointer'|
C:\C Programs\OpenGL Test\main.cpp|39|undefined reference to `_imp____glewDisableVertexAttribArray'|
obj\Debug\main.o||In function `CreateVertexBuffer':|
C:\C Programs\OpenGL Test\main.cpp|55|undefined reference to `_imp____glewGenBuffers'|
C:\C Programs\OpenGL Test\main.cpp|56|undefined reference to `_imp____glewBindBuffer'|
C:\C Programs\OpenGL Test\main.cpp|57|undefined reference to `_imp____glewBufferData'|
obj\Debug\main.o||In function `main':|
C:\C Programs\OpenGL Test\main.cpp|72|undefined reference to `_imp__glewInit'|
C:\C Programs\OpenGL Test\main.cpp|74|undefined reference to `_imp__glewGetErrorString'|
||=== Build finished: 9 errors, 0 warnings (0 minutes, 0 seconds) ===|

(Note, the last two errors about glewInit and glewGetErrorString have more characters on the ends of them, but I can’t post it like that since I get an error about URLs.)

I’ve done several internet searches and it seems most people that get the errors are able to solve them in ways that don’t pertain to me (such as leaving out a header file, etc.)
I’m stumped and am unable to go any further.
If anyone could give some insight how to to get past this, I’d appreciate it!

You have not included the glew library in your link

Thanks for the reply.

I missed including the glew32 library in the linker and added it. However, it only decreased the number of errors from 9 to 7.
Tried a bunch of tutorials on line and have messed around with including different libraries to see if I was leaving anything out. Not much is changing.

The error message still reads:


obj\Debug\main.o||In function `RenderSceneCB':|
C:\C Programs\OpenGL Test\main.cpp|12|undefined reference to `_imp____glewEnableVertexAttribArray'|
C:\C Programs\OpenGL Test\main.cpp|13|undefined reference to `_imp____glewBindBuffer'|
C:\C Programs\OpenGL Test\main.cpp|14|undefined reference to `_imp____glewVertexAttribPointer'|
C:\C Programs\OpenGL Test\main.cpp|18|undefined reference to `_imp____glewDisableVertexAttribArray'|
obj\Debug\main.o||In function `CreateVertexBuffer':|
C:\C Programs\OpenGL Test\main.cpp|34|undefined reference to `_imp____glewGenBuffers'|
C:\C Programs\OpenGL Test\main.cpp|35|undefined reference to `_imp____glewBindBuffer'|
C:\C Programs\OpenGL Test\main.cpp|36|undefined reference to `_imp____glewBufferData'|
||=== Build finished: 7 errors, 0 warnings (0 minutes, 0 seconds) ===|

Try building with GLEW_STATIC compile option to link static library

Hrmmm… That was one of the things I already tried (and is still set right now). It didn’t make any difference. Unless, I did it incorrectly. I have “GLEW_STATIC” under the compiler #defines in Code::Blocks.

I don’t use Code::Blocks so can’t help there. Here are the relevant lines from my VS2008 test


PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;FREEGLUT_STATIC;FREEGLUT_LIB_PRAGMAS;GLEW_STATIC"
AdditionalDependencies="OpenGL32.lib freeglut_static.lib glew_static.lib"

This linked your code with no problems

The glew-1.9.0 package that I downloaded didn’t include a glew_static.lib, could that be an issue?

No, that is just the name I used for my copy of the static library. I think the original is called glew32s.lib and glew32sd.lib for debug.

Oh okay. I tried including the glew32s and glew32sd libraries before with no change in results.

Thanks for everyone’s help, but honestly, I’m almost no closer to solving this than I was when I started.
I have no idea how to get this working or what I’m doing wrong.

I’m having a similar, if not an exact same, issue as Miles. I’m on chapter 2 of openglbook.com.

Here’s the build log


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

And my headers.

#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

I’ve even put GLEW_STATIC before glew.h and had the same outcome.

I’m not too sure about static and dynamic linking. Does that solely rest on whether I use glew32.lib or glew32s.lib? Or should they be in my compiler linker settings vs. my project linker settings?

Using win7 X64 with Code::Blocks and mingw/GCC compiler and fresh installs less than a week ago btw.

I know the error has something to do with the linkers cause I had to figure out that I was accidentally using the 64 bit of glew with 32 bit freeglut and opengl32. Which caused similar issues.

What am I doing wrong?

Trying to use an GLEW library built for MSVC with gcc. Apparently the two aren’t compatible.

There is an unofficial (and dated) version of GLEW for MinGW here, or you can build it from source.

[QUOTE=GClements;1252007]Trying to use an GLEW library built for MSVC with gcc. Apparently the two aren’t compatible.

There is an unofficial (and dated) version of GLEW for MinGW “url” or you can build it from source.[/QUOTE]

I found an article regarding that topic. And compiled glew myself using msys and mingw in 32 bit. But unfortunately it ended up with the same results. Only without the warnings about the VC++ specific commands listed. I’m going to try reinstalling code::blocks, redownloading freeglut and recompiling glew, then see how that works out.

Apparently glu32 had to be added. I thought freeglut or glew would have added that DLL. But nope!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.