Monday, September 6, 2010

How to Compile a GLUT Application in Eclipse

After a week of research, trail and error, and plenty of building and cleaning, I've finally figured out how to get GLUT to compile in Eclipse. Here are the steps that I used, however, unlike most of the "I finally got this to work so here is how I did it" posts I found out there that described this process, I will include as many relevant links as I can at the end of this post. I will also try to explain why we are doing the steps, so that if you have to trouble-shoot it yourself you will understand what you are doing:

1. Download Eclipse Wascana. This is an Eclipse IDE that is already set up with the C++ plugins, the CDT (C/C++ Development Tooling) and the correct version of MinGW (a C++ compiler) and the tools that it uses. This will install in your C: directory and will also add your mingw directory (C:\Wascana\mingw) to the system path. By downloading Wascana you're saving somewhere around 5-10 additional installation steps :)

2. Start Wascana. Go to File -> New -> C++ Project. Under Project Type go to Executable -> Empty Project. IMPORTANT: Under Toolchains select MinGW GCC. If MinGW GCC is not selectable and you are only showing toolchains that are supported in your system, then you probably didn't follow step 1, and you installed MinGW incorrectly on your own version of Eclipse. You will need to reinstall at the above MinGW site.

3. Create a new .cpp file in the root directory of your project. For a quick test I used this one.

4. Download GLUT 3.7.6 here.

5. Once you've unpacked the zip file (containing the .h, .lib and .dll, not the source files) you will want to create a new directory in the root of your C++ project called 'GL'. Copy the glut.h file there. MinGW will also need a copy of glut.h in order to compile correctly. Copy glut.h to C:\Wascana\mingw\include\GL.

6. Optionally, you can create another directory in the root of your project called 'lib'. Add the glut.lib file you just unpacked in your GLUT download to the directory However, this does not add the library to your project! THIS PART IS NOT OPTIONAL: To add the library to your project right click on the project and go to properties -> C/C++ Build -> Settings. Then under Tool Settings go to MinGW C++ Linker -> Libraries. Add glut32, glu32, opengl32 one at a time to the Libraries. You will need to do this for every GLUT project that you create. Also, MinGW comes with the opengl and glu libraries, but you will need to specify where the glut32 library is stored. Add a new Library search path below the libraries. Press the "File System..." button and navigate to and add the lib directory we just created, or add the directory where you chose to keep the glut32.lib file.

7. Now add the glut32.dll to a directory where your system will look for it. For XP users, put it in 'C:\Windows\System32'. if you are using a Vista or Windows 7, my understanding is that you should put it in 'C:\Windows\SysWOW64'. For my Windows 7 machine I put it in both directories, just to be safe. *shrug* Also, you will need to add the .dll file to the root of your C++ project.

8. If you cleaned your code now, there is a possibility that your code might work fine. However myself and many of the people on the forums seemed to get an error at this point that said something like:

undefined reference to `__glutCreateWindowWithExit'

If you get this, then you will need to add one or both of the following lines to your .cpp file before you include glut.h:

#define GLUT_DISABLE_ATEXIT_HACK
#include <windows.h>

Now you should be able to clean your project without any errors. When I tried to build it would say that there was nothing to build, but after cleaning I could right click and run as a Local C++ app.

If you continue to have problems, here are some sites that I found helpful:


Also, I tried to get GLUI to compile in Eclipse. It's impossible. GLUI was built using Microsoft Visual C, and therefore does not get along with MinGW. I'm not sure if there's a way you can build the source using MinGW, but for as many times as I saw this mentioned as a possible solution by the know it alls of the development forums to anyone who was trying to get it to work, I never saw anyone who claimed to be successful at it. But then again, I hadn't seen anyone who had attempted to try it either. I gave it a shot and it quickly looked like if it was possible that it was going to take just as long if not longer to figure out as getting GLUT to work, so for the sake of my sanity and marriage, I didn't pursue it any further than that.

There are supposedly newer and better tools that can be used instead of GLUI. If I find a good substitute, I will post it here.

Good Luck!