0

So I've been following a tutorial and when I tried to compile the below code:

#include <glut.h>
#include <iostream>

void render(void);
void keyboard(unsigned char c, int x, int y);
void mouse(int button, int state, int x, int y);

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(640, 480);
    glutCreateWindow("Test GLUT App");

    glutDisplayFunc(render); // render
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutMainLoop(); // initialization finished. start rendering
}

void render(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
    glColor3f(0.5, 0.2, 0.9);
    glVertex2f(-0.5, -0.5);
    glColor3f(0.1, 0.2, 0.5);
    glVertex2f(0.0, -0.5);
    glColor3f(0.3, 0.9, 0.7);
    glVertex2f(0.0, 0.5);
    glEnd();

    glutSwapBuffers();  
}

void keyboard(unsigned char c, int x, int y)
{
    if(c == 27)
    {
        exit(0);
    }
}

void mouse(int button, int state, int x, int y)
{
    if(button == GLUT_RIGHT_BUTTON)
    {
        exit(0);
    }
}

I get 3 errors out of nowhere:

Error 1 error C2381: 'exit' : redefinition; __declspec(noreturn) differs c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdlib.h 353

Error 2 error C3861: 'exit': identifier not found ....main.cpp 45

Error 3 error C3861: 'exit': identifier not found ....main.cpp 53

Does anyone see why this error appears? Im using VS2010.

jogojapan
  • 68,383
  • 11
  • 101
  • 131

4 Answers4

7

You need to #include <cstdlib>.

edit:

You are probably following a very known tutorial that provides a header file for you.

This will help you then GLUT exit redefinition error

Community
  • 1
  • 1
RedX
  • 14,749
  • 1
  • 53
  • 76
  • If GLUT is declarting `exit` in the global namespace, it's a bad sign with regards to the quality of the library. – James Kanze Jul 23 '12 at 07:31
  • @JamesKanze Bad or not it is (i think) the only one available out there that makes entry easy for beginners. – RedX Jul 23 '12 at 07:37
  • @RedX I don't see where it helps anyone. If the library needs `std::exit`, it should include ``. Defining `exit` in global namespace results in undefined behavior, and is _never_ correct. – James Kanze Jul 23 '12 at 07:51
  • @JamesKanze the library itself is not C++ only C because opengl is only C. So there is no options to define namespaces. But IMHO you are right. I have no idea why it defines an `exit` function. – RedX Jul 23 '12 at 08:17
2

If your Visual Studio throws a build error saying IntelliSense did not identify 'exit' then you must include process.h

flare111
  • 21
  • 1
0

Try adding using namespace std to the top. I'm not sure if this will fix it but i had a similar error earlier and that fixed it. good luck.

abk
  • 301
  • 2
  • 4
  • 16
0

u need to declare header simple as work , works in my comp

#include <stdlib.h>
#include <cstdlib>
#include <glut.h>
#include <iostream>
Jefferson X Masonic
  • 583
  • 2
  • 12
  • 27