How to get GLUT code running via xCode?

Hello,
I'm having trouble writing C code with Glut with XCode. The main.c file compiles with no errors, but the 'black icon' file required to 'execute' the same is in red and gives the following errors. All help is very much appreciated.

<code>

Building target “cs6bz9lx01_drawing” of project “cs6bz9lx01_drawing” with configuration “Debug” — (11 errors)
cd /Users/gabriele/Documents/Personale/UNIBZ/cs6bz9/cs6bz9lx/cs6bz9lx01_drawing
setenv MACOSX DEPLOYMENTTARGET 10.5
/Developer/usr/bin/gcc-4.0 -arch i386 -isysroot /Developer/SDKs/MacOSX10.5.sdk -L/Users/gabriele/Documents/Personale/UNIBZ/cs6bz9/workspace/Debug -F/Users/gabriele/Documents/Personale/UNIBZ/cs6bz9/workspace/Debug -filelist /Users/gabriele/Documents/Personale/UNIBZ/cs6bz9/workspace/cs6bz9lx01 drawing.build/Debug/cs6bz9lx01_drawing.build/Objects-normal/i386/cs6bz9lx01drawing.LinkFileList -mmacosx-version-min=10.5 -o /Users/gabriele/Documents/Personale/UNIBZ/cs6bz9/workspace/Debug/cs6bz9lx01_dra wing
Undefined symbols:
"_glEnd", referenced from:
_drawGLScene in main.o
"_glFlush", referenced from:
_drawGLScene in main.o
"_glutDisplayFunc", referenced from:
_main in main.o
"_glutMainLoop", referenced from:
_main in main.o
"_glClearColor", referenced from:
_drawGLScene in main.o
"_glColor3f", referenced from:
_drawGLScene in main.o
"_glBegin", referenced from:
_drawGLScene in main.o
"_glVertex2f", referenced from:
_drawGLScene in main.o
_drawGLScene in main.o
_drawGLScene in main.o
_drawGLScene in main.o
"_glutInit", referenced from:
_main in main.o
"_glutCreateWindow", referenced from:
_main in main.o
"_glClear", referenced from:
_drawGLScene in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
"_glEnd", referenced from:
_drawGLScene in main.o
"_glFlush", referenced from:
_drawGLScene in main.o
"_glutDisplayFunc", referenced from:
_main in main.o
"_glutMainLoop", referenced from:
_main in main.o
"_glClearColor", referenced from:
_drawGLScene in main.o
"_glColor3f", referenced from:
_drawGLScene in main.o
"_glBegin", referenced from:
_drawGLScene in main.o
"_glVertex2f", referenced from:
_drawGLScene in main.o
_drawGLScene in main.o
_drawGLScene in main.o
_drawGLScene in main.o
"_glutInit", referenced from:
_main in main.o
"_glutCreateWindow", referenced from:
_main in main.o
"_glClear", referenced from:
_drawGLScene in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Build failed (11 errors)

#include <stdio.h>
#include <GLUT/GLUT.h>

void drawGLScene(){
glClearColor(1,1,1,0); // white background color (0,0,0,0) is black
glColor3f(0,0,0); //drawings past this will be in black
glClear(GL COLOR_BUFFERBIT); // clears the screen with the selected background color (was 7th line)
glBegin(GL LINESTRIP); //glBegin(...) starts drawing the parameter, GL LINESTRIP means that points defined afterwards shall be joined by straight line
glVertex2f(-0.5,0.0); //defines vertex at x = -0.5 and y = 0,0;
glVertex2f(0.5,0.0);
glVertex2f(0.0,0.7);
glVertex2f(-0.5,0.0);
glEnd(); //stop drawing
glFlush(); //draws objects defined above on to the screen
// glutPostRedisplay(); start drawing again (animation)
}

int main (int argc, char **argv) {
glutInit(&argc, argv); // initializes glut and starts using it
glutCreateWindow("Simple OpenGL Example");
glutDisplayFunc(drawGLScene); //draw as per function above
glutMainLoop(); // loop for keep on drawing.
return 0;
}
</code>

Macbook, Mac OS X (10.5.5)

Posted on Feb 26, 2009 6:49 AM

Reply
9 replies

Feb 26, 2009 9:24 AM in response to Simpatico

I can't necessarily solve your problem - but a couple hints:

1) Use matching
tags to display your code correctly.
2) You are clearly missing includes that are triggering all the "Undefined Symbols" messages.
At a minimum I would think you would need to include:

#include <gl.h>
#include <glu.h>
#include <glut.h>
{code}
to get anything GLUT oriented to work.
3) Have you looked at the GLUT example code? It's almost all in straight C.
4) Unless you're really super determined to keep your code C based and platform neutral I would look into setting up a window with an OGL view in it. It is really simple in IB and gives you a lot of stuff for free like resizable windows etc.

There is a lot of example code of both straight C and OS X windowing oriented examples out there to work from.

Good luck,

=Tod

Feb 26, 2009 11:23 AM in response to danielsju6

Hello, adding the following displays errors (neither in capitals)
#include <gl.h>
#include <glu.h>
#include <glut.h>

I did look at sample code I downloaded and found in the developer directory and it just uses the include i used, but this is not the issue.
Those who suggest me to use the above can you point how I can get those frameworks to work?

Lastly I'm developing in C small 'assignments' and ought to stick to them.

Feb 27, 2009 1:00 PM in response to Simpatico

Sorry, the includes should be more like:

#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h> // Header File For The OpenGL32 Library
#include <OpenGL/glu.h> // Header File For The GLu32 Library
#include <GLUT/glut.h> // Header File For The GLut Library


But I'm still not sure the will get you the includes you want in a straight C program. In Cocoa you simply add the OpenGL Framework and then the includes automagically work. Adding the framework made my simple GLUT app compile and run fine, but I don't know if this will prevent you from making your app portable or not.

The easiest way to include a Framework in your build is to right-click on the Frameworks folder and do Add > Existing Frameworks... If you include the Frameworks it should run and give you the triangle drawing you're hoping for.

HTH,

=Tod

Mar 1, 2009 9:36 AM in response to Tod Kuykendall

ya adding them was the answer. Could I expect some GLUT programing help too?

I've been trying to write a C program using GLUT to recursively generate Sierpinski triangles in a a window, but've been met by several problems. Here is the first I encountered:

{code


{
double ax, ay, bx, by, cx, cy, a1x, a1y, b1x, b1y, c1x, c1y, ab; //global variables
void setVertices(double xa, double ya, double aba){
//set global variables
ab = aba;
ax = xa;
ay = ya;
bx = ax + ab;
by = ay;
cx = ax + ab/2;
cy = aba;
ab = bx - ax;
}
//draw triangles using coordinates set above (variables are global)
void drawTriangle(){
glClearColor(1, 1, 1, 0.0);
glColor3f(0,0,0);
glClear(GL COLOR_BUFFERBIT);
glPolygonMode(GL FRONT_ANDBACK, GL_LINE);
glBegin(GL_TRIANGLES);
glVertex2f(ax,ay);
glVertex2f(bx, by);
glVertex2f(cx, cy);
glEnd();
glFlush();
}
//main
int main (int argc, char ** argv) {
glutInit(&argc, argv);
glutCreateWindow("Serpinski Triangle");
setVertices(1,1,5);
glutDisplayFunc(drawTriangle);
glutMainLoop();
return 0;
}
//output no triangle drawn. Where am I wrong?
}

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to get GLUT code running via xCode?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.