Pregunta

So I just started learning OpenGL and I'm doing so Ubuntu with the C language.

I have done a few examples from my lecturers notes and they worked however this one is giving me errors.

callbackexample.c: In function ‘main’:
callbackexample.c:17:18: error: ‘displays’ undeclared (first use in this function)
callbackexample.c:17:18: note: each undeclared identifier is reported only once for each     function it appears in

and so on for every method in my file. I followed his notes word for word and I'm getting this so I'm not sure whats going wrong.

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

#define DEG_TO_RAD 0.017453

int singleb, doubleb;   //window ids
GLfloat theta = 0.0;

int main(int argc, char **argv){

glutInit(&argc, argv);

//create single buffered window
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
singleb = glutCreateWindow("single_buffered");
glutDisplayFunc(displays);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
glutMouseFunc(mouse);
glutKeyboardFunc(mykey);

//create double buffered window
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(400,0); //create window to the right
doubleb = glutCreateWindow("double_buffered");
glutDisplayFunc(displayd);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
glutMouseFunc(mouse);
glutCreateMenu(quit_menu);
glutAddMenuEntry("quit", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);

glutMainLoop();
}


void displays() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
    glVertex2f ( cos(DEG_TO_RAD * theta),
            sin(DEG_TO_RAD * theta));
    glVertex2f ( -sin(DEG_TO_RAD * theta),
            cos(DEG_TO_RAD * theta));
    glVertex2f ( -cos(DEG_TO_RAD * theta),
            -sin(DEG_TO_RAD * theta));
    glVertex2f ( sin(DEG_TO_RAD * theta),
            -cos(DEG_TO_RAD * theta));

glEnd();
glFlush();
}

This is just some code of the main method and the first method after it which I get an error on. By undeclared I guess it means that the methods aren't declared but I followed his code so I'm not sure

¿Fue útil?

Solución

You try to use the displays method in the main function, before declaring it. You need to either move the entire function before the main function or add the stub to the top:

void displays();

int main(int argc, char **argv){
   ...
}

void displays(){
   ...
}

C parses everything in the order it sees it - you cannot use a method that hasn't been declared in its entirety, or at least had a declaration that it will exist at some point.

In regards to your comments: The cannot find -l* stuff you are getting implies that you either haven't installed the development libraries for OpenGL or have them set up strangely - this is saying that it cannot find the library files to link against.

Additionally, the mykey problem implies that you either haven;t declared a mykey function or are not declaring it as per the prototype:

void mykey(unsigned char key, int x, int y) 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top