Glut in Dev C++ error “redeclaration of C++ built-

2020-04-30 03:16发布

问题:

I am beginner to GLUT in C++. I am using Dev C++ as my IDE. I have this simple triangle drawing code and it produces an error "redeclaration of C++ built-in type short ". But When I put #include<iostream.h> before #include<glut.h>, it compiles and runs. Can anyone explain the logic behind that?

#include<glut.h>
void renderScene(void) {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0.0);
        glVertex3f(0.5,0.0,0.0);
        glVertex3f(0.0,0.5,0.0);
    glEnd();

        glutSwapBuffers();
}

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

    // init GLUT and create Window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("My first program");

    // register callbacks
    glutDisplayFunc(renderScene);

    // enter GLUT event processing cycle
    glutMainLoop();

    return 1;
}

回答1:

Hard to say without seeing your exact glut.h and library versions, but I see roundabout line 45 of glut.h:

   /* XXX This is from Win32's <ctype.h> */
#  ifndef _WCHAR_T_DEFINED
typedef unsigned short wchar_t;
#   define _WCHAR_T_DEFINED
#  endif

If wchar_t is already defined (to short for example), but the _WCHAR_T_DEFINED macro is not, the line will then be treated as:

typedef unsigned short short;

Which is a redeclaration of the built-in type. <iostream> (don't use the .h btw, it's not used anymore per standard) is adding defines such that the typedef is not executed, or undef'ing wchar_t if it is a macro such that the typedef is legal.



回答2:

I faced error redeclaration of c++ built-in type 'wchar_t' at the time of my car racing project. I searched in Google, but didn't find any solution to solve my problem :-(

But later, I solved this problem by including "windows.h" by myself :-)

#include<windows.h>
#include<bits/stdc++.h>
#include<GL/glut.h>

#include<windows.h> has to be added at the top. If it's added under glut.h there will be an error.

#include<bits/stdc++.h> just added for safety :-p

Use #include<GL/glut.h> or #include<glut.h> depending on the folder where glut.h has been placed.