Problem running simple GLUT/OpenGL program in Xcode

Discussion in 'Mac Programming' started by kashmoney2006, Apr 16, 2008.

  1. macrumors regular

    Joined:
    Dec 12, 2007
    #1
    So I have to learn GLUT for a project and I and trying to follow a tutorial. the problem is, when I run the code found in the tutorial, nothing happens? Why is this? Here is the code I am trying to get to work:

    Code:
    #include <stdlib.h>
    
    #include <GLUT/glut.h>
    
    void display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        glutSwapBuffers();
    }
    
    void reshape(int width, int height)
    {
        glViewport(0, 0, width, height);
    }
    
    void idle(void)
    {
        glutPostRedisplay();
    }
    
    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
        glutInitWindowSize(640, 480);
        
        glutCreateWindow("GLUT Program");
        
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutIdleFunc(idle);
        
        glutMainLoop();
        return EXIT_SUCCESS;
    }
    
    It compiles without any errors or warnings but when it runs nothing happens. The tutorial says that a black window should pop up, but no window here?? Does anybody have any suggestions? I am running Xcode 3.1 on Mac OS 10.5.2 and the tutorial is here:
    http://blog.onesadcookie.com/2007/12/xcodeglut-tutorial.html

    I have also tried to compile and run code from other sources but I get the same result. Any help is appreciated. Thanks.
     
  2. macrumors 65816

    Muncher

    Joined:
    Apr 19, 2007
    Location:
    California
    #2
    I don't think the tutorial is to fault, I used it a couple months ago and it worked for me.

    The general steps of the tutorial are this.

    1. Make a new (empty) project.
    2. Right-click (ctrl-click) on the top icon (project icon) under the "Groups & Files" column in the new project window. Select New Target. Click Carbon Application.
    3. The target settings will pop up. Find the box which says: "Base SDK Path," and double click. A box will scroll down. Paste (if it isn't already there) "$(DEVELOPER_SDK_DIR)/MacOSX10.5.sdk" (no quotes). If the line in the box ends with "MacOSX10.4u.sdk" switch ".4u" to ".5". For some reason if you use the 10.4 sdk, it won't work (just my experience, it probably works but it didn't for me).
    4. Right-click (ctrl-click) on the top icon (project icon) under the "Groups & Files" column in the new project window. Select Existing Frameworks. Find and Command-click the following frameworks (folders):

    Application Services
    Carbon
    Core Services
    GLUT
    Hit Add. It will ask you if you want to add them to the target you created. Hit yes.
    5. Right-click (ctrl-click) on the top icon (project icon) under the "Groups & Files" column in the new project window. Select New File. Click either C or C++ file, or if you want to use Carbon, Carbon C or Carbon C++ (I recommend Carbon C++, you get the greatest flexibility with it.)
    6. Paste your code into the C/C++ file you created.
    7. In the Project window, hit Build and Go.

    It should work! Let us know if you still have problems.
     
  3. m85
    macrumors newbie

    Joined:
    Apr 8, 2008
    #3
    You may also need to add OpenGL.framework.

    I also had some trouble with a simple GLUT app a few days ago. My code would compile but would refuse to link the gl functions (glut functions were fine). It was a very basic Xcode 3.1 project with just main.c, GLUT.framework and the target (shell tool). This had previously worked for me in Xcode 2.4 so I got really stressed until I thought of adding OpenGL.framework and it worked.

    Hope this helps.
     
  4. thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #4
    Thanks! I had to also add OpenGL.framework, but once i did that, it worked like a charm! Why did i choose Carbon Application? In the tutorial it said to choose Cocoa application, do you think that was the problem? or do you think it is the 3 other frameworks that you had me add (Application Services, Carbon, and Core Services)?
     
  5. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    The only frameworks you need are GLUT and OpenGL. I just make a standard C command line project as a Carbon application project seems a bit overkill really.
     
  6. macrumors 65816

    Muncher

    Joined:
    Apr 19, 2007
    Location:
    California
    #6
    Actually, you don't have to, but the button() function in Carbon is pretty handy for small demos :D.

    @Cromulent: I thought you needed CoreServices, etc. I've always just added them automatically. Oh well. :p
     
  7. macrumors member

    Joined:
    Feb 18, 2008
    Location:
    Los Alamos, New Mexico (Atomic City)
    #7
    Okay. I'm having a similar problem.

    I'm an absolute beginner at OpenGL/GLUT in C. I've managed to compile programs properly in Ubuntu, but when I try to compile using gcc in Mac, it gives me the following error message:

    /usr/bin/ld: Undefined symbols:
    _glClear
    _glClearColor
    _glColor3f
    _glColorMaterial
    _glEnable
    _glLightfv
    _glLoadIdentity
    _glMatrixMode
    _glRotatef
    _glTranslatef
    _gluPerspective
    _glutCreateWindow
    _glutDisplayFunc
    _glutIdleFunc
    _glutInit
    _glutInitDisplayMode
    _glutInitWindowPosition
    _glutInitWindowSize
    _glutKeyboardFunc
    _glutMainLoop
    _glutSolidSphere
    _glutSwapBuffers
    collect2: ld returned 1 exit status

    (Or, basically, all of the OpenGL/GLU/GLUT calls that I try to make.)

    I've tried compiling with Xcode as well, but it gives me essentially the same error. I'm using Tiger 10.4.11. Also, when I change the target settings for the 10.4 SDK, it tells me that the SDK doesn't exist.
    What am I doing wrong?
     
  8. thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #8
    did you include the libraries in your code? did you use GL/glut.h and GL/glu.h or GLUT/glut.h and OpenGL/glu.h? also, did you link the frameworks to your project? the OpenGL and GLUT frameworks should be linked to you target in Xcode. how big is your project? if you would like to send it to me I could try compiling it myself.
     
  9. m85
    macrumors newbie

    Joined:
    Apr 8, 2008
  10. macrumors member

    Joined:
    Feb 18, 2008
    Location:
    Los Alamos, New Mexico (Atomic City)
    #10
    Well, it works now. I thought that I had already tried this, but after I added the GLUT and OpenGL frameworks and included GL/gl.h, GL/glu.h, and GL/glut.h, it started working. Thanks, everybody!
     
  11. macrumors newbie

    Joined:
    Nov 23, 2009
    #11
    Exc_bad_access

    My problem sounds similar but the advice hasn't worked. the error log says It experiences an "EXC_BAD_ACCESS". I am trying to use CFDate/data/string and have added the frameworks for those, could that be causing some kind of conflict with the console application?
     
  12. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #12
    That sounds like you are trying to use a NULL pointer to me.
     

Share This Page