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:

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:


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

Why can’t I have void there?

  1. 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


gluPerspective(45,ratio,1,1000);

and


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:


# 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:


/* 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(&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

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

hi pjcozzi

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

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++.

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

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

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?


-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


#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.

Oh, sorry that I didn’t mention that. I didn’t know there’s anything else than MinGW, I’ve got that one.

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

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.

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.