Results 1 to 7 of 7

Thread: Problem with glu / confusion with void, int main

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    4

    Problem with glu / confusion with void, int main

    Hi

    I'm new to this forum and I'm learning C++ and OpenGL. I'm using Windows XP, texteditor, latest GCC and Gnu Make versions. While I've got basic knowledge of C++ (or even less), OpenGL is completely new to me.
    I'm following this tutorial (http://www.lighthouse3d.com/opengl/glut/index.php?3), which is, by the way, very good.

    But I'm puzzled about two things:

    1. In the tutorial the main function is written as void:
    Code :
    void main(int argc, char **argv) {
    But my gcc compiler gives errors here: "'main' must return 'int'" and "return type for 'main' changed to 'int'".

    I then changed the function into int:
    Code :
    int main(int argc, char **argv) {
    Why can't I have void there?

    2. If I compile my code with that changed line I get zero errors. But when I try to run the compiled .exe file I get a windows error:
    The application failed to initialize properly - 0xc0000005
    |OK|

    When I comment the lines
    Code :
    gluPerspective(45,ratio,1,1000);
    and
    Code :
    gluLookAt(0.0,0.0,5.0, 
    0.0,0.0,-1.0,
    0.0f,1.0f,0.0f);
    the error doesn't occur anymore, but the window has bugs while resizing. Is this error a problem with the glu library?
    I'm using the following makefile:
    Code :
    # A simple Makefile
    # C++ using GNU Make and GCC
    # opengl32.lib glut32.lib glu32.lib odbc32.lib odbccp32.lib
     
    myprogram.exe : myfile.cpp
    	gcc myfile.cpp -Wall -lopengl -lglu -lglut32

    this is my code:
    Code :
    /* hallo.c */
     
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
     
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GLUT/glut.h>
     
    using namespace std;
     
    void renderScene(void) {
    	glClear(GL_COLOR_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();
    	glFlush();
    }
    void changeSize(int w, int h) {
     
    	// Prevent a divide by zero, when window is too short
    	// (you cant make a window of zero width).
    	if(h == 0)
    		h = 1;
     
    	float ratio = 1.0* w / h;
     
    	// Reset the coordinate system before modifying
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
     
    	// Set the viewport to be the entire window
    	glViewport(0, 0, w, h);
     
    	// Set the correct perspective.
    	gluPerspective(45,ratio,1,1000);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    	gluLookAt(0.0,0.0,5.0, 
    		      0.0,0.0,-1.0,
    			  0.0f,1.0f,0.0f);
    }
     
     
    int main(int argc, char **argv) {
    	glutInit(&amp;argc, argv);
    	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
    	glutInitWindowPosition(100,100);
    	glutInitWindowSize(320,320);
    	glutCreateWindow("3D Tech- GLUT Tutorial");
    	glutDisplayFunc(renderScene);
     
    	glutReshapeFunc(changeSize);
     
    	glutMainLoop();
    }

    I'd appreciate any help from you.

    Kenji

  2. #2
    Senior Member Regular Contributor imported_pjcozzi's Avatar
    Join Date
    Jun 2004
    Location
    Philadelphia, PA
    Posts
    196

    Re: Problem with glu / confusion with void, int main

    Quote Originally Posted by Kenji
    But my gcc compiler gives errors here: "'main' must return 'int'" and "return type for 'main' changed to 'int'".
    I haven't used gcc in a long time but either you get this error by default or it is because of your -Wall option. I believe it is considered a better practice to explicitly return an int since a user can write a script (e.g. a .bat file) that calls your program and checks for error codes. I even recall a corny saying: "friends don't let friends void main." BTW Visual C++ 2008 Express Edition is free and may be easier to use depending on your preferences, not that I want to steer you away from using makefiles, etc, since that is a great skill to have.

    Regards,
    Patrick

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    4

    Re: Problem with glu / confusion with void, int main

    hi pjcozzi

    Thank you for your answer. Since "int main" is better practice anyways, I'll avoid "void" main in future.

    Quote Originally Posted by pjcozzi
    ...or it is because of your -Wall option
    No, it's not because of that. I removed it in the makefile and there's still the same error. It seems to be forbidden in C and C++.

    Quote Originally Posted by pjcozzi
    BTW Visual C++ 2008 Express Edition is free and may be easier to use depending on your preferences, not that I want to steer you away from using makefiles, etc, since that is a great skill to have.
    I have Visual C++ Express edition, I also have Dev C++, I tried them out. My problem with IDEs is that I lose the overview. With gcc and text editor you know what you have. No hidden buttons and options in the IDE you might overlook or anything like that. In addition, I want to be OS independent.

    Regards,
    Kenji

  4. #4
    Senior Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    408

    Re: Problem with glu / confusion with void, int main

    I use gcc on both Linux and Windows. It is a great tool for just the reasons you mentioned and more

    Definitely avoid the use of "void main" since it is not ANSI standard coding style.

    What compile tools are you using -- cygwin or mingw/msys?

    Does the change as follows work?
    Code :
    -lopengl32 -lglu32 -lGLUT32
    Those are the link libraries I have to use with mingw/msys commandline environment in windows. Note on linux just "-lglut" is required.

    P.S. If you ever want to be truly cross platform and share code with others. A simple consistent change will make that possible
    Code :
    #ifdef WINDOWS
    #include <windows.h>
    #endif
     
    and glut.h by standard convention goes in GL/ folder
    #include <GL/glut.h>
    With those two coding style changes your code will compile as-is on windows and Linux alike. You don't need to do this but I just point it out because it is a minor change that allows you to be cross platform in the long run. Also similar changes may be needed to compile demos from windows/linux/glut code you may run across in the net as you learn openGL. Even though glut is cross platform by design some coders put extraneous OS specific code in that breaks cross platform ability needlessly -- the simple #IFDEF undoes that.

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    4

    Re: Problem with glu / confusion with void, int main

    Quote Originally Posted by marshats
    What compile tools are you using -- cygwin or mingw/msys?
    Oh, sorry that I didn't mention that. I didn't know there's anything else than MinGW, I've got that one.
    Quote Originally Posted by marshats

    Does the change as follows work?

    Code :
    -lopengl32 -lglu32 -lGLUT32
    Those are the link libraries I have to use with mingw/msys commandline environment in windows.
    You are right! It's working! I don't know why and how it finds the "opengl32" and "glu32" library. It seems that they already came with MinGW, but the files are named "libopengl32.a" and "libglu32.a". Thank you very much.

    Of course I'll take your advice and I'll use these lines for future programs.

    Regards,
    Kenji

  6. #6
    Senior Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    408

    Re: Problem with glu / confusion with void, int main

    Glad that helped. I think they did come with your mingw/msys installation. Note that when you specify -lFOO gcc automatically searches its library path and uses the first found file named libFOO.a ie by adding the prefix "lib" and postfix ".a".

    Not that you need it now but a nice reference to bookmark is GCC library path. It is a nice reference that I go back to sometimes when I install new libraries etc.

  7. #7
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    4

    Re: Problem with glu / confusion with void, int main

    Thank you for that link. I've read pieces of these informations all over the internet and it's really nice to see them collected on one page.
    I've already tried out some custom library directories and it actually worked. Cool stuff.

Similar Threads

  1. Difference between GLvoid and void
    By Shelling in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-15-2011, 01:43 PM
  2. Problem with marker confusion
    By aki-hime in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 10-27-2011, 11:30 AM
  3. Problem with FBO->PBO->main memory transfer
    By Blizzeye in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 11-26-2009, 03:14 PM
  4. int - void????
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-17-2004, 11:47 PM
  5. OT: C++inline void or void
    By DaViper in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 01-31-2001, 06:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean