1

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;
}
genpfault
  • 47,669
  • 9
  • 68
  • 119
Umair Ayub
  • 13,220
  • 12
  • 53
  • 124
  • Is the primary question "why does it work when I put iostream.h first"? – wallyk Apr 11 '13 at 20:33
  • @wallyk This would be better title! But I need the reason why it works? – Umair Ayub Apr 11 '13 at 20:37
  • I suggest you have a look at the preprocessor output of both versions, maybe you can spot the reason there. – Daniel Frey Apr 11 '13 at 20:39
  • Please post the complete compiler invocation and output. – engineerC Apr 11 '13 at 20:43
  • 2
    I'm not sure about your problem, but iostream is not a .h file, you should use '#include ' without '.h' at the end. – Kevin Apr 11 '13 at 20:49
  • @CaptainMurphy **1 C:\Dev-Cpp\triangle.cpp In file included from triangle.cpp 45 C:\Dev-Cpp\include\glut.h redeclaration of C++ built-in type short C:\Dev-Cpp\Makefile.win [Build Error] [triangle.o] Error 1** – Umair Ayub Apr 11 '13 at 20:52
  • Compile log **Compiler: Default compiler Building Makefile: "C:\Dev-Cpp\Makefile.win" Executing make... make.exe -f "C:\Dev-Cpp\Makefile.win" all g++.exe -c triangle.cpp -o triangle.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" In file included from triangle.cpp:1: C:/Dev-Cpp/include/glut.h:45: error: redeclaration of C++ built-in type `short' make.exe: *** [triangle.o] Error 1 Execution terminated ** – Umair Ayub Apr 11 '13 at 20:52
  • @Kevin It is working code but I was just wondering **why does it work when I put iostream.h first** – Umair Ayub Apr 11 '13 at 20:54
  • Consider using a modern C++ compiler, e.g. [GCC](http://gcc.gnu.org/) – Basile Starynkevitch Mar 13 '21 at 17:35

3 Answers3

3

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.

engineerC
  • 2,648
  • 13
  • 29
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.

vich
  • 11,638
  • 13
  • 46
  • 60
Joyhasan
  • 191
  • 1
  • 5
  • *#include just added for safety* Aargh! Don't ever recommend using that header: [Why should I not #include ?](https://stackoverflow.com/q/31816095/10871073) – Adrian Mole Mar 13 '21 at 19:19
0

I have faced the same problem and change the wchar_t variable name to wchar_tt in the "glut.h" file and it works fine.

#  ifndef _WCHAR_T_DEFINED
typedef unsigned short wchar_tt;
#   define _WCHAR_T_DEFINED
#  endif
# endif