OpenGL glutInit 2

Sid Kraft 96 Reputation points
2026-08-01T02:09:07.6133333+00:00
#include <iostream>
#include<Gl\gl.h>
#include<GL\glut.h>
#include<GL\freeglut.h>
#include<cmath>
using namespace std;
int main()
{
    void display(void)
    {
        glClearColor(0.f, 0.f, 0.f, 1.f);
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        glFlush();
    }
    glutInit();
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Hello World");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
    return 0;
}

Here is a very simple set of codes to open a window in opengl code which I created to prove a point. It was said that there were other things going on in my large program that caused an error right after the void display(void) where the "{" was issuing an error saying "expected a ;" I still get this error even with this small subset of my larger program. So, in effect, there is some other issue causing the error, not sure what it is, Sid Kraft

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Answer accepted by question author

AI answer

2026-08-01T02:09:20.7+00:00

The error is caused by defining display inside main. In standard C++, a function definition cannot be nested inside another function, so the compiler reports a syntax error at the { after void display(void).

Move display out of main, then register it with glutDisplayFunc and glutIdleFunc.

#include <iostream>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <cmath>

using namespace std;

void display()
{
    glClearColor(0.f, 0.f, 0.f, 1.f);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Hello World");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
    return 0;
}

Also correct these points:

  1. glutInit is normally called with arguments: glutInit(&argc, argv);
  2. Header casing and paths should be consistent, for example #include <GL/gl.h> rather than mixing Gl and GL.
  3. glLoadIdentity and glFlush are valid OpenGL calls in the display function.

References:

AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

3 people found this answer helpful.

5 additional answers

Sort by: Most helpful
  1. Jay Pham (WICLOUD CORPORATION) 3,905 Reputation points Microsoft External Staff Moderator
    2026-08-03T00:32:56.6333333+00:00

    Hi @Sid Kraft ,

    Glad the issue is resolved. Just to add a bit more context for future reference:

    Disclaimer: The links above point to third-party websites that are not owned or controlled by Microsoft. I'm providing them for reference only. Microsoft does not guarantee the accuracy, completeness, or availability of any information on these sites, and does not provide support for third-party products or libraries.

    • The root cause is a language rule: in C/C++, a function definition may only appear at file (namespace) scope, so display() cannot be defined inside main(). Nested declarations are allowed, but nested definitions are not. Reference: Function definitions – nested functions are not allowed
    • I'd also suggest not including both <GL/glut.h> and <GL/freeglut.h> in the same file. freeglut already ships freeglut_std.h as a drop-in replacement for glut.h, so including both can cause redefinition warnings. Reference: freeglut API documentation
    • Minor note: glutIdleFunc(display) will redraw continuously and keep the CPU busy. If the scene is static, you can remove it and call glutPostRedisplay() only when something changes.

    I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback. Thank you. 

      

    Was this answer helpful?

    1 person found this answer helpful.

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Roma Khedr 5 Reputation points
    2026-08-03T01:55:35.0766667+00:00

    This code serves as the basic boilerplate template for opening a graphical window using OpenGL and GLUT in C++. It initializes the window, sets the background color to black, and continuously redraws it.🟢✅

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.