今天写一个OpenGL的程序时发现我的机器出现这样一个链接错误,link时出现的错误提示:
error LNK2001: unresolved external symbol ___glutInitWithExit@12

这是一个很常见的问题,解决办法很简单:

下载新版本的glut32库。

我把这个库附在文章后面供大家下载了。

更新库后继续编译链接,发现又出来一个错误:

error LNK2001: unresolved external symbol _WinMain@16

原来,我用VC建的不是一个Console的工程,只需要把工程重新建为Win32 Console Application,再把代码复制进去就可以了。

好事做到底,我在博文后面附上了OpenGL的一个较为完整的开发库,不过其中glut32的版本较低,只需要使用我附件中的glut32更新库替代就可以了。

送佛送到西,顺带讲解一下OpenGL在VC下的环境设置:

1、将开发库中的.h文件拷贝到Visual C++ 6.0的\Include\GL目录中
2、将.lib文件拷贝到Visual C++ 6.0的\lib目录中
3、将.dll文件拷贝到操作系统的\WINDOWS\system32目录中

OpenGL的功能相当强大,最后附上一个旋转的三角形的例子:

 

 
  
  1. /**************************  
  2.  * Includes  
  3.  *  
  4.  **************************/ 
  5.  
  6. #include <windows.h>  
  7. #include <gl/gl.h>  
  8.  
  9.  
  10. /**************************  
  11.  * Function Declarations  
  12.  *  
  13.  **************************/ 
  14.  
  15. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,  
  16. WPARAM wParam, LPARAM lParam);  
  17. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);  
  18. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);  
  19.  
  20.  
  21. /**************************  
  22.  * WinMain  
  23.  *  
  24.  **************************/ 
  25.  
  26. int WINAPI WinMain (HINSTANCE hInstance,  
  27.                     HINSTANCE hPrevInstance,  
  28.                     LPSTR lpCmdLine,  
  29.                     int iCmdShow)  
  30. {  
  31.     WNDCLASS wc;  
  32.     HWND hWnd;  
  33.     HDC hDC;  
  34.     HGLRC hRC;          
  35.     MSG msg;  
  36.     BOOL bQuit = FALSE;  
  37.     float theta = 0.0f;  
  38.  
  39.     /* register window class */ 
  40.     wc.style = CS_OWNDC;  
  41.     wc.lpfnWndProc = WndProc;  
  42.     wc.cbClsExtra = 0;  
  43.     wc.cbWndExtra = 0;  
  44.     wc.hInstance = hInstance;  
  45.     wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);  
  46.     wc.hCursor = LoadCursor (NULL, IDC_ARROW);  
  47.     wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);  
  48.     wc.lpszMenuName = NULL;  
  49.     wc.lpszClassName = "GLSample";  
  50.     RegisterClass (&wc);  
  51.  
  52.     /* create main window */ 
  53.     hWnd = CreateWindow (  
  54.       "GLSample""OpenGL Sample",   
  55.       WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,  
  56.       0, 0, 256, 256,  
  57.       NULL, NULL, hInstance, NULL);  
  58.  
  59.     /* enable OpenGL for the window */ 
  60.     EnableOpenGL (hWnd, &hDC, &hRC);  
  61.  
  62.     /* program main loop */ 
  63.     while (!bQuit)  
  64.     {  
  65.         /* check for messages */ 
  66.         if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))  
  67.         {  
  68.             /* handle or dispatch messages */ 
  69.             if (msg.message == WM_QUIT)  
  70.             {  
  71.                 bQuit = TRUE;  
  72.             }  
  73.             else 
  74.             {  
  75.                 TranslateMessage (&msg);  
  76.                 DispatchMessage (&msg);  
  77.             }  
  78.         }  
  79.         else 
  80.         {  
  81.             /* OpenGL animation code goes here */ 
  82.  
  83.             glClearColor (0.0f, 0.0f, 0.0f, 0.0f);  
  84.             glClear (GL_COLOR_BUFFER_BIT);  
  85.  
  86.             glPushMatrix ();  
  87.             glRotatef (theta, 0.0f, 0.0f, 1.0f);  
  88.             glBegin (GL_TRIANGLES);  
  89.             glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);  
  90.             glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);  
  91.             glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);  
  92.             glEnd ();  
  93.             glPopMatrix ();  
  94.  
  95.             SwapBuffers (hDC);  
  96.  
  97.             theta += 1.0f;  
  98.             Sleep (1);  
  99.         }  
  100.     }  
  101.  
  102.     /* shutdown OpenGL */ 
  103.     DisableOpenGL (hWnd, hDC, hRC);  
  104.  
  105.     /* destroy the window explicitly */ 
  106.     DestroyWindow (hWnd);  
  107.  
  108.     return msg.wParam;  
  109. }  
  110.  
  111.  
  112. /********************  
  113.  * Window Procedure  
  114.  *  
  115.  ********************/ 
  116.  
  117. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,  
  118.                           WPARAM wParam, LPARAM lParam)  
  119. {  
  120.  
  121.     switch (message)  
  122.     {  
  123.     case WM_CREATE:  
  124.         return 0;  
  125.     case WM_CLOSE:  
  126.         PostQuitMessage (0);  
  127.         return 0;  
  128.  
  129.     case WM_DESTROY:  
  130.         return 0;  
  131.  
  132.     case WM_KEYDOWN:  
  133.         switch (wParam)  
  134.         {  
  135.         case VK_ESCAPE:  
  136.             PostQuitMessage(0);  
  137.             return 0;  
  138.         }  
  139.         return 0;  
  140.  
  141.     default:  
  142.         return DefWindowProc (hWnd, message, wParam, lParam);  
  143.     }  
  144. }  
  145.  
  146.  
  147. /*******************  
  148.  * Enable OpenGL  
  149.  *  
  150.  *******************/ 
  151.  
  152. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)  
  153. {  
  154.     PIXELFORMATDESCRIPTOR pfd;  
  155.     int iFormat;  
  156.  
  157.     /* get the device context (DC) */ 
  158.     *hDC = GetDC (hWnd);  
  159.  
  160.     /* set the pixel format for the DC */ 
  161.     ZeroMemory (&pfd, sizeof (pfd));  
  162.     pfd.nSize = sizeof (pfd);  
  163.     pfd.nVersion = 1;  
  164.     pfd.dwFlags = PFD_DRAW_TO_WINDOW |   
  165.       PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;  
  166.     pfd.iPixelType = PFD_TYPE_RGBA;  
  167.     pfd.cColorBits = 24;  
  168.     pfd.cDepthBits = 16;  
  169.     pfd.iLayerType = PFD_MAIN_PLANE;  
  170.     iFormat = ChoosePixelFormat (*hDC, &pfd);  
  171.     SetPixelFormat (*hDC, iFormat, &pfd);  
  172.  
  173.     /* create and enable the render context (RC) */ 
  174.     *hRC = wglCreateContext( *hDC );  
  175.     wglMakeCurrent( *hDC, *hRC );  
  176.  
  177. }  
  178.  
  179.  
  180. /******************  
  181.  * Disable OpenGL  
  182.  *  
  183.  ******************/ 
  184.  
  185. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)  
  186. {  
  187.     wglMakeCurrent (NULL, NULL);  
  188.     wglDeleteContext (hRC);  
  189.     ReleaseDC (hWnd, hDC);  
  190. }