Setting up CodeBlocks with GLUT for Windows

My favorite C++ IDE is a free program called CodeBlocks. It has a nice set of features, and is very extensible and configurable. To top it off, it’s cross-platform, which means that if you move to another operating system, you can still use the same exact IDE you’ve grown familiar with.

Due to the amount of times I’ve changed or upgraded operating systems (currently Windows 7 Pro), I’ve had to set up CodeBlocks and GLUT a few times. Problem is, it’s usually been long enough that I forget all the details. I end up unnecessarily wasting time searching Google to work through all the niggling problems that come from forgetting to set up the link libraries, or copy headers properly. Thus, I’ve made this post partly as a reminder to myself for any future times I do this, and partly to help anyone else out there who might run into the same problem.

  1. Go to CodeBlocks.org, click on Downloads, and follow the link for the binary version. You want the file with “mingw” in the name, unless you plan on using your own compiler.
  2. Get the GLUT zip file, either from Nate Robins’ page or from OpenGL.org
  3. Install CodeBlocks, and unzip the GLUT zip file to somewhere easy like the desktop.
  4. Move the glut32.dll file from your desktop to your \windows\system directory.
  5. Move the glut32.lib file from your desktop to the \CodeBlocks\MinGW\lib directory.
  6. Move the glut.h file from your desktop to the \CodeBlocks\MinGW\include\GL directory.
  7. Start up CodeBlocks, and click on Settings then Compiler and Debugger.
  8. Click the tab that says Linker Settings.
  9. Underneath the box titled “Link Libraries” click the button that says Add and enter the location of your glut32.lib in the dialog that pops up. (Mine looks like this: C:\Program Files (x86)\CodeBlocks\MinGW\lib\glut32.lib)

That’s it for the setup! To test it, fire up CodeBlocks, click File, New, Project, and choose GLUT Project from the screen that appears. When you click Build and Run, you may get the following error:

error: redeclaration of C++ built-in type `short’

If so, you need to add the line #include <windows.h> to your code at the top. See the example below:


/*
 * GLUT Shapes Demo
 *
 * Written by Nigel Stewart November 2003
 *
 * This program is test harness for the sphere, cone
 * and torus shapes in GLUT.
 *
 * Spinning wireframe and smooth shaded shapes are
 * displayed until the ESC or q key is pressed.  The
 * number of geometry stacks and slices can be adjusted
 * using the + and - keys.
 */

#include <windows.h>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>

Not really sure why this is, and a quick Google search didn’t prove terribly conclusive. All I know is that including the windows.h header file in your code will stop the error.

Advertisements

2 thoughts on “Setting up CodeBlocks with GLUT for Windows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s