Program does not exit!

  • Follow


I have to use the GLUT_DISABLE_ATEXIT_HACK in order for my program
to link correctly with gcc. However, the executable hangs in the
background even after closing (Task Manager shows hello.exe still running).

How can I make the program exit?

Code sample is from the OpenGL Programming Guide.
Compiling with Dev-C++ version 4.9.9.2.

Any help appreciated.

Chris

#define GLUT_DISABLE_ATEXIT_HACK
#include <windows.h>
#include <GL/glut.h>

void display(void)
{
      glClear(GL_COLOR_BUFFER_BIT);

      glColor3f(100.0, 1.0, 1.0);
      glBegin(GL_POLYGON);
          glVertex3f(0.25, 0.25, 0.0);
          glVertex3f(0.75, 0.25, 0.0);
          glVertex3f(0.75, 0.75, 0.0);
          glVertex3f(0.25, 0.75, 0.0);
      glEnd();

      glFlush();
}

void init(void)
{
      glClearColor(0.0, 0.0, 0.0, 0.0);

      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char * argv[])
{
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
     glutInitWindowSize(250, 250);
     glutInitWindowPosition(100, 100);
     glutCreateWindow("Hello");
     init();
     glutDisplayFunc(display);
     glutMainLoop();

     return 0;
}
0
Reply Fiber 8/21/2005 5:28:14 AM

Fiber Optic wrote in message ...
>I have to use the GLUT_DISABLE_ATEXIT_HACK in order for my program
>to link correctly with gcc. However, the executable hangs in the
>background even after closing (Task Manager shows hello.exe still running).
>
>How can I make the program exit?
>
>Code sample is from the OpenGL Programming Guide.
>Compiling with Dev-C++ version 4.9.9.2.
>
>Any help appreciated.
>Chris

 compiles fine (GNU GCC 3.3.1 MinGW)
 I compiled as C++ win32 console app.

// #define GLUT_DISABLE_ATEXIT_HACK

>#include <windows.h>
>#include <GL/glut.h>
>
>void display(void){
>      glClear(GL_COLOR_BUFFER_BIT);
>      glColor3f(100.0, 1.0, 1.0);
>      glBegin(GL_POLYGON);
>          glVertex3f(0.25, 0.25, 0.0);
>          glVertex3f(0.75, 0.25, 0.0);
>          glVertex3f(0.75, 0.75, 0.0);
>          glVertex3f(0.25, 0.75, 0.0);
>      glEnd();
>      glFlush();
>}
>
>void init(void){
>      glClearColor(0.0, 0.0, 0.0, 0.0);
>      glMatrixMode(GL_PROJECTION);
>      glLoadIdentity();
>      glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
>}


// ========== Keyboard ==========
// Keyboard Handler (Normal Keys)
void Keyboard(unsigned char key, int x, int y) {
     switch (key) {
          case 'q':          // When q Is Pressed...
          case 27: {       // When Escape Is Pressed...
               exit(0);       // Exit The Program
               break;
               }
          default:
               break;
          }
     } // Keyboard()
// ------------------------------


>int main(int argc, char * argv[]){
>     glutInit(&argc, argv);
>     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
>     glutInitWindowSize(250, 250);
>     glutInitWindowPosition(100, 100);
>     glutCreateWindow("Hello");
>     init();
>     glutDisplayFunc(display);

     glutKeyboardFunc(Keyboard); // Register Keyboard Handler

>     glutMainLoop();
>     return 0;
>}

Now you can press 'q' or [escape] to exit.

Double check your 'Parameters' in the project.
Leave out that ugly 'HACK' macro, add the 'Keyboard' handler, compile, and
post[1] the first full error message.

[1] - copy/paste, delete directory-path in error message. Mark any line
numbers in source so we know where error is.
--
Bob R
POVrookie


0
Reply BobR 8/21/2005 8:29:35 PM


> Double check your 'Parameters' in the project.
> Leave out that ugly 'HACK' macro, add the 'Keyboard' handler, compile, and
> post[1] the first full error message.
> 
> [1] - copy/paste, delete directory-path in error message. Mark any line
> numbers in source so we know where error is.
> --
> Bob R
> POVrookie

Thanks Bob.

Without the ugly hack I get the following errors (full output follows):

Compiler: Default compiler
Building Makefile: "F:\tmp\OpenGL\Ch1\Makefile.win"
Executing  make...
make.exe -f "F:\tmp\OpenGL\Ch1\Makefile.win" all
gcc.exe -c main.cpp -o main.o -I"include"

gcc.exe main.o  -o "hello.exe" -L"lib" -L"C:/Dev-Cpp/lib" -mwindows 
-lopengl32 -lglu32 -lglut32

main.o(.text+0x1c):main.cpp: undefined reference to `__glutInitWithExit@12'
main.o(.text+0x3d):main.cpp: undefined reference to 
`__glutCreateWindowWithExit@8'
main.o(.text+0x5d):main.cpp: undefined reference to 
`__glutCreateMenuWithExit@8'
collect2: ld returned 1 exit status

make.exe: *** [hello.exe] Error 1

Execution terminated

Any ideas?

Also, Dev-Cpp, strangley, did not have glut.h in its include directory,
however, it does have the glut32.a library! So I placed a copy of glut.h
from http://www.xmission.com/~nate/glut.html

Thanks,
Chris
0
Reply Fiber 8/21/2005 9:12:26 PM

>  compiles fine (GNU GCC 3.3.1 MinGW)
>  I compiled as C++ win32 console app.
> 
> // #define GLUT_DISABLE_ATEXIT_HACK

I found several references on the internet saying this define
is required. I'd rather not use it if I don't have to.

Here's one example:
http://tinyurl.com/9dtf8

> Now you can press 'q' or [escape] to exit.

The keyboard handler works well. Thanks. However, I don't
understand why the application stays open on a window
close event. This is a bug from my standpoint. Either that,
or something needs to be added to the code to handle this event.

Chris
0
Reply Fiber 8/21/2005 9:22:50 PM

>>  compiles fine (GNU GCC 3.3.1 MinGW)
>>  I compiled as C++ win32 console app.
>>
>> // #define GLUT_DISABLE_ATEXIT_HACK
> 
> 
> I found several references on the internet saying this define
> is required. I'd rather not use it if I don't have to.
> 
> Here's one example:
> http://tinyurl.com/9dtf8
> 
>> Now you can press 'q' or [escape] to exit.
> 
> 
> The keyboard handler works well. Thanks. However, I don't
> understand why the application stays open on a window
> close event. This is a bug from my standpoint. Either that,
> or something needs to be added to the code to handle this event.
> 
> Chris

I found an example OpenGL Win32 app in the Dev-CPP directory which
works really well, and responds to window close events, however, it
does not appear to be using glut :( Code follows. Chris.

/**************************
  * Includes
  *
  **************************/

#include <windows.h>
#include <gl/gl.h>


/**************************
  * Function Declarations
  *
  **************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);


/**************************
  * WinMain
  *
  **************************/

int WINAPI WinMain (HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine,
                     int iCmdShow)
{
     WNDCLASS wc;
     HWND hWnd;
     HDC hDC;
     HGLRC hRC;
     MSG msg;
     BOOL bQuit = FALSE;
     float theta = 0.0f;

     /* register window class */
     wc.style = CS_OWNDC;
     wc.lpfnWndProc = WndProc;
     wc.cbClsExtra = 0;
     wc.cbWndExtra = 0;
     wc.hInstance = hInstance;
     wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
     wc.hCursor = LoadCursor (NULL, IDC_ARROW);
     wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
     wc.lpszMenuName = NULL;
     wc.lpszClassName = "GLSample";
     RegisterClass (&wc);

     /* create main window */
     hWnd = CreateWindow (
       "GLSample", "OpenGL Sample",
       WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
       0, 0, 256, 256,
       NULL, NULL, hInstance, NULL);

     /* enable OpenGL for the window */
     EnableOpenGL (hWnd, &hDC, &hRC);

     /* program main loop */
     while (!bQuit)
     {
         /* check for messages */
         if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
         {
             /* handle or dispatch messages */
             if (msg.message == WM_QUIT)
             {
                 bQuit = TRUE;
             }
             else
             {
                 TranslateMessage (&msg);
                 DispatchMessage (&msg);
             }
         }
         else
         {
             /* OpenGL animation code goes here */

             glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
             glClear (GL_COLOR_BUFFER_BIT);

             glPushMatrix ();
             glRotatef (theta, 0.0f, 0.0f, 1.0f);
             glBegin (GL_TRIANGLES);
             glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
             glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);
             glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);
             glEnd ();
             glPopMatrix ();

             SwapBuffers (hDC);

             theta += 1.0f;
             Sleep (1);
         }
     }

     /* shutdown OpenGL */
     DisableOpenGL (hWnd, hDC, hRC);

     /* destroy the window explicitly */
     DestroyWindow (hWnd);

     return msg.wParam;
}


/********************
  * Window Procedure
  *
  ********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                           WPARAM wParam, LPARAM lParam)
{

     switch (message)
     {
     case WM_CREATE:
         return 0;
     case WM_CLOSE:
         PostQuitMessage (0);
         return 0;

     case WM_DESTROY:
         return 0;

     case WM_KEYDOWN:
         switch (wParam)
         {
         case VK_ESCAPE:
             PostQuitMessage(0);
             return 0;
         }
         return 0;

     default:
         return DefWindowProc (hWnd, message, wParam, lParam);
     }
}


/*******************
  * Enable OpenGL
  *
  *******************/

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
     PIXELFORMATDESCRIPTOR pfd;
     int iFormat;

     /* get the device context (DC) */
     *hDC = GetDC (hWnd);

     /* set the pixel format for the DC */
     ZeroMemory (&pfd, sizeof (pfd));
     pfd.nSize = sizeof (pfd);
     pfd.nVersion = 1;
     pfd.dwFlags = PFD_DRAW_TO_WINDOW |
       PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
     pfd.iPixelType = PFD_TYPE_RGBA;
     pfd.cColorBits = 24;
     pfd.cDepthBits = 16;
     pfd.iLayerType = PFD_MAIN_PLANE;
     iFormat = ChoosePixelFormat (*hDC, &pfd);
     SetPixelFormat (*hDC, iFormat, &pfd);

     /* create and enable the render context (RC) */
     *hRC = wglCreateContext( *hDC );
     wglMakeCurrent( *hDC, *hRC );

}


/******************
  * Disable OpenGL
  *
  ******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
     wglMakeCurrent (NULL, NULL);
     wglDeleteContext (hRC);
     ReleaseDC (hWnd, hDC);
}
0
Reply Fiber 8/21/2005 9:28:57 PM

Fiber Optic wrote in message ...
>>
>
>main.o(.text+0x1c):main.cpp: undefined reference to `__glutInitWithExit@12'
>main.o(.text+0x3d):main.cpp: undefined reference to
>`__glutCreateWindowWithExit@8'
>main.o(.text+0x5d):main.cpp: undefined reference to
>`__glutCreateMenuWithExit@8'
>collect2: ld returned 1 exit status
>
>Any ideas?
>
>Also, Dev-Cpp, strangley, did not have glut.h in its include directory,
>however, it does have the glut32.a library! So I placed a copy of glut.h
>from http://www.xmission.com/~nate/glut.html
>Thanks,
>Chris

Looks like something is declared in *that* 'glut.h' that is not defined in
the libraries. The headers are tied to the library, they MUST match. If you
use a different header, get the library for it too.

patient: Doctor, I can't use a glut 3.7 header with my glut 3.6 library!
doctor: Well, don't do that!
(gad, that's a sick twist on an old joke! <G>)

--
Bob R
POVrookie


0
Reply BobR 8/21/2005 10:09:37 PM

> Looks like something is declared in *that* 'glut.h' that is not defined in
> the libraries. The headers are tied to the library, they MUST match. If you
> use a different header, get the library for it too.
> 
> patient: Doctor, I can't use a glut 3.7 header with my glut 3.6 library!
> doctor: Well, don't do that!
> (gad, that's a sick twist on an old joke! <G>)

Thanks Bob. Since you can compile with gcc... where did you get your 
glut from?

I tried the Dev-Pak for glut from here:
http://www.nigels.com/glt/devpak/

But I get all sorts of errors after installing into Dev-C++,
and trying to build with it.

Chris
0
Reply Fiber 8/21/2005 10:33:58 PM

> Looks like something is declared in *that* 'glut.h' that is not defined in
> the libraries. The headers are tied to the library, they MUST match. If you
> use a different header, get the library for it too.
> 
> patient: Doctor, I can't use a glut 3.7 header with my glut 3.6 library!
> doctor: Well, don't do that!
> (gad, that's a sick twist on an old joke! <G>)


Also, I uninstalled whatever Dev-C++ version I had installed,
and installed version 4.9.9.2 of Dev-C++, and it comes with both
the GL and GLUT libraries, including the glut.h header, with the hack!

Chris
0
Reply Fiber 8/21/2005 10:36:27 PM

Fiber Optic wrote:
>
> Compiler: Default compiler
> Building Makefile: "F:\tmp\OpenGL\Ch1\Makefile.win"
> Executing  make...
> make.exe -f "F:\tmp\OpenGL\Ch1\Makefile.win" all
> gcc.exe -c main.cpp -o main.o -I"include"
>
> gcc.exe main.o  -o "hello.exe" -L"lib" -L"C:/Dev-Cpp/lib" -mwindows
> -lopengl32 -lglu32 -lglut32
>
> main.o(.text+0x1c):main.cpp: undefined reference to `__glutInitWithExit@12'
> main.o(.text+0x3d):main.cpp: undefined reference to
> `__glutCreateWindowWithExit@8'
> main.o(.text+0x5d):main.cpp: undefined reference to
> `__glutCreateMenuWithExit@8'
> collect2: ld returned 1 exit status
>
> make.exe: *** [hello.exe] Error 1
>
> Execution terminated
>
> Any ideas?

has the same problem. try to find glut library for mingw.

0
Reply night 8/22/2005 5:27:54 AM

8 Replies
289 Views

(page loaded in 0.139 seconds)

Similiar Articles:













7/28/2012 8:09:53 AM


Reply: