Thread: Debugging help

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Debugging help

    Can anyone help me debug my program?

    I am programming in C on a Mac.

    here are the errors I am getting


    ball2.c: In function ‘main’:
    ball2.c:276: warning: passing argument 1 of ‘glutMouseFunc’ from incompatible pointer type
    Undefined symbols:
    "_drawText", referenced from:
    _displayFunction in ccHqckS2.o
    _displayFunction in ccHqckS2.o
    "_initialize", referenced from:
    _main in ccHqckS2.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You need to link in the GLut or OpenGL library. Consult the documentation for the library and/or the development environment you're using to find out how to do this.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    I am a novice programmer so I am sorry if I need a little extra help.

    I included GLUT and OpenGL libraries at the beginning of my programs.

    Should I post my code?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's a difference between compiler and linker. You haven't included any libraries with a line like #include <GLUT.h>; you've included a header file. You need to get the actual code behind the header file, and that's what the linker is there to do. On the Mac (or at least the last time I used GL on the Mac) you end up using frameworks (Foundation, OpenGL, and GLUT if you're using that). If you're in Xcode, there's a little box for that, somewhere, otherwise you need to modify your command line/makefile.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    I did include the GLUT library.
    #include <GLUT/GLUT.h>

    I am using xcode, but just an editor.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    I have had to use the box for linking libraries in xcode before, but recently I've been working around all of that, by working strait out of the editor.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Here is my code. All I'm trying to do is animate a bouncing ball. If someone can help me get this running I would really appreciate it.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <OpenGL/gl.h>
    #include <GLUT/GLUT.h>


    #define ZERO .0000001

    #define WIDTH 600 /* window dimensions */
    #define HEIGHT 600

    typedef struct Physics
    {
    float x, y;
    }Vector;

    Vector vi, vf, di, df, bounceV;

    float mx=0.0, my=0.0; /* Coordinates of center of circle */
    float angle=0.0;
    float h, cor;
    float time,dt = .01;
    float A = 0; //x direction acceleration
    float G = -9.8; //acceleration of gravity
    //float e = 0.9; //bounce decay

    int animate = 0;
    char velocitystr [10];


    void intialize (void) //initial parameters for all variables
    {
    h = 400;
    vi.x = 0;
    vi.y = 0;
    vf.x = 0;
    vf.y = 0;
    di.x = 0;
    di.y = 0;
    df.x = 0;
    df.y = 0;
    time = 0;
    animate = 0;
    }

    int collisions(void) //Parameters to keep ball bouncing within the given screen
    {
    if (di.y < ZERO)
    {
    bounceV.y = (1*cor*vi.y);
    bounceV.x = vi.x;

    if (bounceV.y < ZERO) //bounce for the y direction
    {
    bounceV.y = bounceV.y * -1; // reverse the y velocity
    }
    return 1;
    }
    return 0;
    }


    void physics(void) //Physics of ball for simulation
    {
    if (df.y < -2)
    {
    animate = 0;
    }
    vf.x = vi.x + (dt * A);
    vf.y = vi.y + (dt * G);
    df.x = di.x + (dt * vf.x);
    df.y = di.y + (dt * vf.x);

    if (collisions() == 1) //collisions call back to continue animating ball
    { //when it reaches the edge of the screen
    vf.x = bounceV.x + (dt *A);
    vf.y = bounceV.y + (dt *G);
    df.x = df.x + (dt * bounceV.x);
    df.y = df.y + (dt * bounceV.y);
    }
    vi = vf; //store the new values into the current values
    di = df;

    glutPostRedisplay();

    }

    void drawCircle(float radius, int segments)
    {
    /* Draw a circle with the specified radius and number of segments */
    int i;
    glBegin(GL_POLYGON);
    for (i=0; i < segments; i++)
    {
    float x = radius * cos(i*2.0*M_PI/segments);
    float y = radius * sin(i*2.0*M_PI/segments);
    glVertex2f(x,y);
    }
    glEnd();
    }

    void drawrect()
    {
    /* Draw a Rectangle */

    glBegin(GL_POLYGON);
    glVertex2i(0, 5);
    glVertex2i(WIDTH/4, 5);
    glVertex2i(WIDTH/4, -5);
    glVertex2i(0, -5);
    glEnd();
    }

    void drawtext(char * string) //draw text function
    {
    int len = strlen(string);
    int i;
    glRasterPos2f(0,0);
    for (i = 0; i < len; i++)
    {
    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, string[i]);
    }

    }

    void displayFunction(void)
    {
    /* Clear the screen and draw circles */

    glClear(GL_COLOR_BUFFER_BIT); /* clear the window to the background color */

    glLineWidth(1.0);
    glColor3f(1,0,0);

    glPushMatrix(); //draws circle
    glTranslatef(di.x + WIDTH/4,di.y,0); //allows circle height to track mouse
    drawCircle(20,20);
    glPopMatrix();


    glLineWidth(1.0);
    glColor3f(0,1,0); //color green

    glPushMatrix(); //draws rectangle
    glTranslatef(5,h-15,0);
    drawrect();
    glPopMatrix();

    glPushMatrix(); //draw text
    glTranslatef(0, HEIGHT - 20, 0);
    glColor3f(0,0,0); //color black
    drawText("X speed =");
    glTranslatef(134,0,0);
    drawText(velocitystr);
    glPopMatrix();

    glFlush();
    glutSwapBuffers();
    }

    void mouseFunction(int button, int state, int x, int y)
    {
    if (animate == 0)
    {
    //if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
    if ( button == GLUT_LEFT && state == GLUT_DOWN )
    {
    h = HEIGHT - y;
    di.y = HEIGHT - y;
    glutPostRedisplay();
    }
    }
    }


    void motionFunction(int x, int y)
    {
    if (animate == 0) //adjust ball and ledge with the mouse
    {
    h = HEIGHT - y;
    di.y = HEIGHT - y;
    glutPostRedisplay();

    }
    }

    void keyboardFunction(unsigned char key, int x, int y)
    {
    if (animate == 0)
    {
    if (key = 's') //begin simutlation with 's' key
    {
    animate = 1;
    }
    else if (key == 'r') //reset simulation with 'r' key
    {
    initialize();
    glutPostRedisplay();

    }
    }

    if (key == 'p') //pause simulation with 'p' key
    {
    if (animate == 1)
    {
    animate = 0;
    }
    if (animate == 0) //unpause with 2nd press
    {
    animate = 1;
    }
    }
    }


    void arrows(int key, int x, int y)
    {
    if (animate == 0)
    {
    if (key == GLUT_KEY_UP) //increase ball x velocity by 5 with up arrow press
    {
    vi.x += 5;
    }
    else if (key == GLUT_KEY_DOWN) //decrease ball x velocity by 5 with up arrow press
    {
    vi.x -= 5;
    }
    }
    }

    void idle(void)
    {
    sprintf(velocitystr, "%.2f", vi.x); //vaule of x velocity for draw text
    sprintf(velocitystr, "%.2f", vi.y); //value of y velocity for draw text


    if (animate == 1)
    {
    time += dt;
    physics(); //
    }
    glutPostRedisplay();
    }

    int main(int argc, char* argv[])
    {
    if (argc < 2)
    {
    printf("COR please");
    exit(0);
    }
    cor = atof(argv[1]);
    //to run
    //./Physics .833

    initialize();
    /* start up the glut utilities */
    glutInit(&argc, argv);

    /* create the graphics window, giving width, height, and title text */

    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutCreateWindow("Logan Jinks ~ Bouncing Ball");

    /* the routine to call to draw graphics when glutMainLoop() is called */

    glutDisplayFunc(displayFunction);

    /* Test the program by enabling one of the callbacks to see how it functions */

    //funcution callbacks
    glutMouseFunc(mouseFunction);
    glutMouseFunc(motionFunction);
    glutKeyboardFunc(keyboardFunction);
    glutSpecialFunc(arrows);
    glutIdleFunc(idle);
    //set screen origin to bottom left corner
    gluOrtho2D(0, WIDTH, 0, HEIGHT);
    glClearColor(1, 1, 1, 0);

    /* start main loop */

    glutMainLoop();
    return 0;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by logan_jinx View Post
    I did include the GLUT library.
    #include <GLUT/GLUT.h>
    You can repeat yourself, but that doesn't make what you say true. That line DOES NOT link in the GLUT library. It gives you the header file, but not the library.

    If you are compiling from the command line, you need (at least) -framework OpenGL -framework GLUT added in as options.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    yes I am compiling in the command line

    my compiling code

    gcc -o shapes ball2.c -framework OpenGL -framework GLUT

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    That 276 in the compiler errors is the line number. That should put you right on top of one instance of the problem; notice, though, that if you discover a problem in a certain place, and that idea was used earlier in a program, sometimes the error had its birthplace before the error occurred.

    To find out which line is which number, you can CAT a copy of the program with line numbers. For convenience, copy the program to another filename. Then, <CODE>CAT -n secondfilename > numberedfilename [ENTER] </CODE>. This will produce a stdout numbered copy of the source. Then you can open up the numbered copy, scroll down until you hit the right line number, and find the place where the error was cited.

    Using this method, I was able to determine that the error is near the end. The 277 line (where I got the error in compiling without that glut thing) is the second to last comment, which reads, " <CODE> /* Test the program by enabling one of the callbacks to see how it functions */ </CODE>"

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    I know what line my problem occurs on but I just don't understand what my error means or how to fix the problem

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    mouseFunction and motionFunction aren't the same type, so they can't both fit inside glutMouseFunc.

  13. #13
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    If mouseFunction and motionFunction aren't the same type, what do I do to fix this?

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This is probably a simple case of not seeing the issue because the functions are named so similarly. When you're registering your callbacks, don't you want this
    Code:
    glutMouseFunc(motionFunction);
    to be
    Code:
    glutMotionFunction(motionFunction);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help debugging in Pelles C IDE
    By gaurav9991 in forum C Programming
    Replies: 3
    Last Post: 10-30-2010, 07:15 AM
  2. Questions about debugging with Visual Studio
    By CodeKate in forum C Programming
    Replies: 4
    Last Post: 08-18-2010, 12:51 PM
  3. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  4. Problem in debugging in Eclipse
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 08-21-2007, 09:53 AM
  5. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM

Tags for this Thread