Linking Error with GLUT

I am using the latest version of MinGW with Code::Blocks 8.02 along with OpenGL 3.1 and GLUT 3.7.

Normally, for GLUT/OpenGL projects, I use Microsoft Visual Studio to compile. I’m looking into cross platform development, however, and so I’ve decided to move many of my OS Portable projects to using GCC 4.4.0 with MinGW to modify the code to work with it. Actually, that was about a month ago.

This is my first time using GLUT on GCC and I’ve tried and failed what feels like 100 times. For every function I use in GLUT, I get a linking error along the lines of:

…\obj\glut.o:glutlib.c:(.text+0x45b)||undefined reference to __glutInitWithExit'| ..\obj\glut.o:glutlib.c:(.text+0x483)||undefined reference toglutInitDisplayMode’|
…\obj\glut.o:glutlib.c:(.text+0x4d8)||undefined reference to __glutCreateWindowWithExit'| ..\obj\glut.o:glutlib.c:(.text+0x5a7)||undefined reference toglutInitWindowSize’|
…\obj\glut.o:glutlib.c:(.text+0x60e)||undefined reference to glutDisplayFunc'| ..\obj\glut.o:glutlib.c:(.text+0x68e)||undefined reference toglutIdleFunc’|
…\obj\glut.o:glutlib.c:(.text+0x70e)||undefined reference to glutKeyboardFunc'| ..\obj\glut.o:glutlib.c:(.text+0x78e)||undefined reference toglutReshapeFunc’|
…\obj\glut.o:glutlib.c:(.text+0x7cc)||undefined reference to glutMainLoop'| ..\obj\glut.o:glutlib.c:(.text+0x7ec)||undefined reference toglutPostRedisplay’|
…\obj\glut.o:glutlib.c:(.text+0x80c)||undefined reference to `glutSwapBuffers’|
||=== Build finished: 11 errors, 0 warnings ===|

Yes, I’ve linked in all the libraries needed for GLUT and OpenGL. OpenGL actually works. It seems to link fine…just GLUT doesn’t.

I thought it might be a problem with the libraries I built from source. I obtained another set from online pre-built for MinGW and had the exact same result. I tried with the libraries built as glut32.lib instead of libglut32.a and still had the same result…even after changing the linked library from glut32 to glut32.lib. I even tried changing the “linked libraries” section and putting them into Code::Blocks directly as command line arguments (-lopengl32 -lglut32) and had the same result.

Since I’m not using anything from GLU, I didn’t link the library. I thought that might be the problem and added it in. Of course, it made absolutely no difference. I also tried linking in a whole set of other libraries like winms, gdi and other things I’ve never had to link but that still made no difference.

The following is the command line:
-pg -pedantic -Wmain -O3 -lglut32 -lopengl32

There were also the following pre-defined definitions which I don’t know how to put in the command line:

WIN32
_DEBUG
_WINDOWS

NOTE: OpenGL was already present in MinGW. I added in GLUT which did not come with the package. Could it be a version mis-match? Doesn’t sound like it from the errors. SDL which didn’t come with MinGW works fine with OpenGL.

My code is strictly closed source (at least for now) but I can post small snippets which I doubt is necessary since this is a linking error.

Using Windows XP SP3 32 bit.

One thing that looks suspicious to me is the “…\obj\glut.o” part of your error message. Are you trying to link directly to the .o file from the original source rather than the compiled library file that you should have installed in your linkers search path?

It may be worth a shot – but try reversing the order of opengl32 and glut32 and capitalizing as GLUT32
“-pg -pedantic -Wmain -O3 -lopengl32 -lGLUT32”

A quick google search lead to setting up GLUT with code::blocks which may be helpful. This is probably a better approach.

On your second question, I don’t use code blocks but gcc allows you to define settings on the command line with the “-D” for instance you mentioned
WIN32
_DEBUG
_WINDOWS
so you could try for instance -D_DEBUG to compile in debug mode.
ie “-pg -pedantic -Wmain -O3 -lopengl32 -lGLUT32 -D_DEBUG”. Please note that this will not have anything to do with the linking error but will allow you to use global #DEFINE’s in the source code.

I’m linking to the .a file. The glut.o file was something created from my own code. It’s now glutlib.o.

To try and debug the problem lets try with something simple. Can you compile and link the most basic glut code following without errors from within code:clocks:


#include <windows.h> //if your on windos
#include <GL/glut.h>

int main( int argc, char *argv[] )
{
   glutInit( &argc, argv );
   glutInitWindowSize(100, 100);

   return 0;
}

also here is a more up to date url on setting up code:clocks with glut that you should try if the above fails

I copy and pasted the libglut32.a from cygwin and now it seems to work. A while later, I had trouble linking GLU but that was confined to one project only. I fixed it by deleting the glu32.dll library from .\bin. No idea how that would affect anything since a bad glu32.dll would only be a problem at runtime giving exceptions (at least from my experience). Strangely, it still doesn’t work on cygwin; but I’m using MinGW so that doesn’t matter. Of course, one problem fixed, another comes. This time, with windres.exe. It always gives me a problem about having “-x -c after last input file” and then gives me an error saying the file is not found. Of course, the file it says it can’t find is THE FILE WITHOUT ANY PATH SEPARATORS! So it ends up saying something like “gcc: file or directory not found: EProjectsCIresource.rc not found.” I’ll post that somewhere else though since it doesn’t appear to be an OpenGL problem.

not gl related, but for the missing path separator, use forward slashes (unix style : /path/to/dir/ ) instead of backward slashes (dos style : \path o\dir )

an added comment on UNIX style and windows style path conventions: a common hurdle for people coming from windows is the use of the space charachter " " in the path/filename and what to do with the “:” part of the path specification. To use a space you must “escape” it with a backslash (). And their is nothing special about the “:” it needs to be replaced with the normal UNIX path separator “/”

For example the exemplary windows applications folder


c:\Program Files\

in MinGS/msys is translated to


c/Program\ Files/

A possible alternative (I am not to familiar with Code::blocks so you may have to test the following) but you may be able to simply specify the windows path in quotes as follow:


c:\Program Files\

as


"c:\Program Files\"

with the quotes explicitly added.

I had a similar problem getting GLUT to play with MinGW gcc just a week or so ago. I posted my issues/resolution on the MinGW mail list.

The glut32.dll, which I presume you will be linking to, is likely the one from Kilgard’s website? It needed an import library which I tried in vain to build. Eventually I found one on the web named libglut32win.a – which instantly solved my linking problems.
The Kilgard dll has undecorated __stdcall names which doesn’t help the task of creating a good glut32.def file. With some editing, based on your linker errors, it should be possible to make a good .def file.

I was happy I got things linking but figured I’d try a ‘somewhat’ cleaner solution. I decided to try freeglut. I found a freeglut.dll (from version 2.4.0) and made an import library for it.

pexports freeglut.dll &gt; freeglut.def
dlltool -d freeglut.def -l freeglut_win.a

This works fine.