I use this blog as a soap box to preach (ahem... to talk :-) about subjects that interest me.

Sunday, February 6, 2011

GLUT in C with Eclipse on the Mac

For the past couple of months, for my book on Sudoku, I have been writing C programs with Eclipse running under Mac OS.

OpenGL (Open Graphics Library) is allegedly “the premier environment for developing portable, interactive 2D and 3D graphics applications”. I simply had to try it out. I quickly discovered that, beside the generic OpenGL libraries, I also needed GLUT (OpenGL Utility Toolkit, pronounced as in gluttony).

As it turned out, every Macintosh comes with everything you need (Windows systems don’t include GLUT). Therefore, the only thing I needed to do was to tell Eclipse where to find the libraries.

I hit at once a little problem: when you tell Eclipse to use a library (I’ll tell you in a moment how to do it), Eclipse actually looks for it after attaching to its name the string lib at the beginning and the extension .dylib at the end. So, for example, if you tell Eclipse to use the library named GLUT, Eclipse looks for libGLUT.dylib. The problem arises because on the Mac the GLUT library is simply named GLUT.

To fix it, you need to go to the GLUT directory:

cd /system/Library/Frameworks/GLUT.framework/

and then, as root, create the following soft link:

ln –s GLUT libGLUT.dylib

To tell Eclipse where to look for the openGL libraries, open the Project Properties dialogue and select Settings item of the MacOS X C Linker category, as shown in the following figure.

Then, as shown, add the following two entries to the Library search path (-L):


and the following three entries to the Libraries (-l):


After telling Eclipse where the OpenGL/GLUT libraries are, before you can use them in your programs, you still need to tell Eclipse where to find the corresponding header files. For this, still within the same dialogue, select the Includes item of the GCC C Compiler category, as shown below.

When you are there, add the two following entries in the field Include paths (-l):


That’s it. Now you can use the graphics package in C within Eclipse. Here is the small program I used to test the package, copied directly from Eclipse:

#include <stdlib.h>
#include <GL/glut.h>

#define SIDE 306
#define CELL SIDE/9
#define EDGE 8

char *arg;
int use_arg = 0;

void tick(void) {

void output(int k, int j, char c) {
    int x = EDGE + CELL * k + (CELL >> 1) - 5;
    int y = EDGE + CELL * j + (CELL >> 1) + 7;
    glRasterPos2f(x, y);
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, c);

void display(void) {
    if (use_arg) {
         for (int k = 0; k < 81; k++) {
              int kR = k / 9;
              int kC = k-kR*9;
              if (arg[k] != '0') {
                   output(kR, kC, arg[k]);
     else {
         for (int k = 0; k < 9; k++) {
              for (int j = 0; j < 9; j++) {
                    output(k, j, '0'+k);

     for (int k = 0; k <= 9; k++) {
         if (k % 3 == 0) {
              glLineWidth (1.5);
         else {
              glLineWidth (0.5);
         glBegin (GL_LINES);
         glVertex2f (EDGE, EDGE+CELL*k);
         glVertex2f (EDGE+SIDE, EDGE+CELL*k);
         glEnd ();
         glBegin (GL_LINES);
         glVertex2f (EDGE+CELL*k, EDGE);
         glVertex2f (EDGE+CELL*k, EDGE+SIDE);
         glEnd ();

void reshape(int w, int h) {
     glViewport(0, 0, w, h);
     gluOrtho2D(0, w, h, 0);

int main(int argc, char **argv) {
     arg = argv[1];
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
     glutInitWindowSize(SIDE+EDGE+EDGE, SIDE+EDGE+EDGE);
     use_arg = (argc > 0);
     glutCreateWindow((argc > 1) ? argv[2] : "Sudoku");
     glClearColor(1, 1, 1, 1);
     glColor3f(0, 0, 0);
     return EXIT_SUCCESS;

It accepts a string of 81 characters as the first argument and displays it within a Sudoku-like grid. Here is an example of what you get:

Obviously, as there are no events (e.g. user inputs) to trigger any change, this program is pretty dull. But it proves that you can use GLUT in the C programs you develop with Eclipse.


  1. I have a problem. Eclipse shows me this error message every time when I try to compile my code: "ld: library not found for -lglut" I did exactly what you describes in this tutorial. I'm using Mac OS 10.7. Can you help me out with this?

  2. Clearly, it worked for me when I wrote this posting. I am a bit surprised that it says "-lglut", because nowhere "glut" was linked.

    I just noticed that the two "include" at the beginning of the listing were not displayed. I hadn't noticed. Blogspot's editor must have gotten confused by the angle brackets. I just fixed it. Check that you include:

    The GL includes are a bit fiddly. You might have typed in the wrong one.

    In any case, I checked out one of my C programs that use glut and "discovered" that in "Library search path (-L)" I replaced

    And in the "Includes", I replaced

    Additionally, under the main heading "Mac OS C Linker", where it shows the gcc command, I wrote in the "All options" window

    Now, perhaps you will have a different release of X-Windows. It might not be 11R6. You have to check.

    Please don't ask me why I switched from what I wrote in the posting to what I am doing now. I don't have a clue. I usually keep a log of everything I do when installing a new system, but I didn't keep any record of this change.

    I hope this will help you. You can send me an email with snapshots to giulio@giuliozambon.org, although, I am not really an Eclipse expert...