學習Open GL在VS2013編譯過程中遇到的問題

一、VS2013搭建OpenGL環境
1. 下載 OpenGL庫(我本人的網盤)

 OpenGL庫:http://yunpan.cn/ccyvuYTPBU6iL  訪問密碼 e17f

        glut全稱爲:OpenGL Utility Toolkit,也就是OpenGL應用工具包,它建立在OpenGL基礎之上,方便開發OpenGL的某些功能,比如我們後面
要創建的Windows程序,就可以用glut來方便地構建起一個初始化的Windows程序。
2. 安裝glut庫 
說是安裝,其實就是複製。
解壓開這個zip包,會發現其中包含如下幾個文件:
*.h:頭文件,複製到D:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/include/gl目錄下(如果沒有gl目錄,需要自己來新建gl目錄)
*.lib:靜態鏈接庫,複製到D:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/lib目錄下
        *.dll:動態鏈接庫,複製到C:/Windows/System32目錄下,64位操作系統的,還需要複製到C:/Windows/SysWOW64目錄下。
3、創建工程
    (1) 文件-->新建-->項目--》Visual C++-->Win32-->Win32控制檯應用程序-->項目的名稱、位置、解決方案名稱(M)、自己設定-->確定
     
   
(2)確定-->下一步--> 附加選項-->選擇空項目(E)

     

  (3)出現如下畫面

     


  (4)右擊源文件-->添加-->新建項目-->代碼-->C++文件(.cpp)-->名稱中寫入-->main.cpp



(5)測試代碼如下
#include <GL/glut.h>
void myDisplay(void )
{
                 //清除函數,GL_COLOR_BUFFER_BIT表示清除顏色,glClear函數還可以清除其它東西
                glClear( GL_COLOR_BUFFER_BIT );
                 //畫一個矩形。四個參數分別表示了位於對角線上的兩個點的橫、縱座標
                glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
                 //保證前面的OpenGL命令立即執行(而不是讓它在緩衝區中等待)
                glFlush();
}
int main(int argcchar * argv[])
{
                 //對GLUT進行初始化
                glutInit(& argc , argv );
                 //設置顯示方式,GLUT_RGB表示使用RGB顏色、GLUT_INDEX表示使用索引顏色、GLUT_SINGLE表示使用單緩衝、GLUT_DOUBLE表示使用雙緩衝
                glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
                 //設置窗口在屏幕上的位置
                glutInitWindowPosition(100, 100);
                 //設置窗口的大小
                glutInitWindowSize(400, 400);
                 //根據前面設置的信息創建窗口,參數作爲窗口的標題。注意:窗口創建後,並不立即顯示在屏幕上。需要調用glutMainLoop才能看到窗口
                glutCreateWindow( "第一個OpenGL程序" );
                 //當顯示圖畫時,請調用myDisplay函數,於是沒有myDisplay函數就用來畫圖
                glutDisplayFunc(&myDisplay);
                 //進行一個消息循環
                glutMainLoop();
                 return 0;
}

(6)點擊F5、編譯通過,出現如下畫面



二、編譯過程中遇到如下問題如何解決
問題一、
錯誤     1     error LNK2019: 無法解析的外部符號 __imp____glutInitWithExit@12,該符號在函數 _glutInit_ATEXIT_HACK@8 中被引用  
  K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     2     error LNK2019: 無法解析的外部符號 __imp____glutCreateWindowWithExit@8,該符號在函數_glutCreateWindow_ATEXIT_HACK@4 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     3     error LNK1120: 2 個無法解析的外部命令     K:\OpenGL_text\one_text\double_buffer\Debug\double_buffer.exe     1     1    
double_buffer
方法一
          
    在#include<GL/glut.h>頭文件之前加入 #define GLUT_DISABLE_ATEXIT_HACK
方法二:
解決辦法右擊工程屬性:(配置(C): 所有配置-->配置屬性-->C/C++-->預處理器-->預處理器定義-->在預處理器 後面寫入GLUT_BUILDING_LIB
-->應用-->確定


問題二、
錯誤     1     error LNK2019: 無法解析的外部符號 __imp__glBegin@4,該符號在函數 "void __cdecl display(void)" (?display@@YAXXZ) 中被引
用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     2     error LNK2019: 無法解析的外部符號 __imp__glClear@4,該符號在函數 "void __cdecl display(void)" (?display@@YAXXZ) 中被引
用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     3     error LNK2019: 無法解析的外部符號 __imp__glClearColor@16,該符號在函數 "void __cdecl init(void)" (?init@@YAXXZ) 中被引
用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     4     error LNK2019: 無法解析的外部符號 __imp__glColor3f@12,該符號在函數 "void __cdecl display(void)" (?display@@YAXXZ) 中
被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     5     error LNK2019: 無法解析的外部符號 __imp__glEnd@0,該符號在函數 "void __cdecl display(void)" (?display@@YAXXZ) 中被引
用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     6     error LNK2019: 無法解析的外部符號 __imp__glLoadIdentity@0,該符號在函數 "void __cdecl reshape(int,int)" (?
reshape@@YAXHH@Z) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     7     error LNK2019: 無法解析的外部符號 __imp__glMatrixMode@4,該符號在函數 "void __cdecl reshape(int,int)" (?
reshape@@YAXHH@Z) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     8     error LNK2019: 無法解析的外部符號 __imp__glOrtho@48,該符號在函數 "void __cdecl reshape(int,int)" (?
reshape@@YAXHH@Z) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     9     error LNK2019: 無法解析的外部符號 __imp__glPopMatrix@0,該符號在函數 "void __cdecl display(void)" (?display@@YAXXZ)
中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     10     error LNK2019: 無法解析的外部符號 __imp__glPushMatrix@0,該符號在函數 "void __cdecl display(void)" (?display@@YAXXZ)中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     11     error LNK2019: 無法解析的外部符號 __imp__glRotatef@16,該符號在函數 "void __cdecl display(void)" (?display@@YAXXZ) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     12     error LNK2019: 無法解析的外部符號 __imp__glShadeModel@4,該符號在函數 "void __cdecl init(void)" (?init@@YAXXZ) 中被
引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     13     error LNK2019: 無法解析的外部符號 __imp__glVertex2f@8,該符號在函數 "void __cdecl display(void)" (?display@@YAXXZ) 中
被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     14     error LNK2019: 無法解析的外部符號 __imp__glViewport@16,該符號在函數 "void __cdecl reshape(int,int)" (?
reshape@@YAXHH@Z) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     15     error LNK2019: 無法解析的外部符號 _glutInit@8,該符號在函數 "void __cdecl createWindow(int,char * * const)" (?
createWindow@@YAXHQAPAD@Z) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     16     error LNK2019: 無法解析的外部符號 _glutInitDisplayMode@4,該符號在函數 "void __cdecl createWindow(int,char * * const)" (?createWindow@@YAXHQAPAD@Z) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     17     error LNK2019: 無法解析的外部符號 _glutInitWindowPosition@8,該符號在函數 "void __cdecl createWindow(int,char * * const)" (?createWindow@@YAXHQAPAD@Z) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     18     error LNK2019: 無法解析的外部符號 _glutInitWindowSize@8,該符號在函數 "void __cdecl createWindow(int,char * * const)" (?createWindow@@YAXHQAPAD@Z) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     19     error LNK2019: 無法解析的外部符號 _glutMainLoop@0,該符號在函數 _main 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     20     error LNK2019: 無法解析的外部符號 _glutCreateWindow@4,該符號在函數 "void __cdecl createWindow(int,char * * const)" (?
createWindow@@YAXHQAPAD@Z) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     21     error LNK2019: 無法解析的外部符號 _glutPostRedisplay@0,該符號在函數 "void __cdecl spinDisplay(void)" (?
spinDisplay@@YAXXZ) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     22     error LNK2019: 無法解析的外部符號 _glutSwapBuffers@0,該符號在函數 "void __cdecl display(void)" (?display@@YAXXZ) 中
被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     23     error LNK2019: 無法解析的外部符號 _glutDisplayFunc@4,該符號在函數 _main 中被引用    
K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     24     error LNK2019: 無法解析的外部符號 _glutReshapeFunc@4,該符號在函數 _main 中被引用    
K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     25     error LNK2019: 無法解析的外部符號 _glutMouseFunc@4,該符號在函數 _main 中被引用    
K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     26     error LNK2019: 無法解析的外部符號 _glutIdleFunc@4,該符號在函數 "void __cdecl mouse(int,int,int,int)" (?
mouse@@YAXHHHH@Z) 中被引用     K:\OpenGL_text\one_text\double_buffer\double_buffer\main.obj     double_buffer
錯誤     27     error LNK1120: 26 個無法解析的外部命令     K:\OpenGL_text\one_text\double_buffer\Debug\double_buffer.exe    
double_buffer
解決辦法右擊工程屬性:(配置(C): 活動(Debug)-->配置屬性-->連接器-->輸入-->附加依賴項-->在附加依賴項裏面寫入後面寫入
OPENGL32.lib;opengl.lib;GLUT32.lib;glut.lib;GLU32.lib;glu.lib;glew32s.lib;glew32mx.lib;glew32mxs.lib;glew32.lib;GLAUX.lib;(這些庫提前必須
D:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/lib已經存在-->確定



問題三、VMware Workstation與Hyper-V不兼容。請先從系統中移除Hyper-V角色,然後再運行VMware Workstation。
今天在用win8.1的時候發現了這個問題,解決辦法如下
第一步


第二步



第三步重啓系統

問題四、
 無法解析的外部符號_main,該符號在函數_tmainCRTStartup中被調用
在編程時經常遇到“無法解析的外部符號 _main,該符號在函數 ___tmainCRTStartup 中被引用”這樣的錯誤,最近看了一些這方面的書籍,現將解
決方案大概敘述一下:
連接程序在負責連接可執行程序時,選擇相應的c/c++運行時啓動函數。如果設定了/subsystem:windows連接程序開關,那麼連接程序會試圖尋找
WinMain或wWinMain函數。
如果連個函數都不存在,那麼就會返回一個“一個未解決的外部符號”(無法解析的外部符號 _main,該符號在函數 ___tmainCRTStartup 中被引
用)同樣,如果設定了/subsystem:console
連接程序開關,那麼連接程序就會試圖尋找main函數或者wmain函數,如果找不到這連個函數是,連接程序返回unresolved externel symbol錯誤
信息。變成新手容易犯的一個錯誤是,
創建應用程序時選擇了錯誤的應用程序類型。例如,開發人員創建了一個新的Win32應用程序項目,並給這個項目創建了一個入口函數main。在
這種情況下,創建應用程序時,開發人員就
會看到一個連接程序的錯誤信息。這是因爲Win32應用程序項目設置了/subsystem:windows連接程序開關,但是卻找不到相應的Winmain或者
wWinmain函數。這是侯你可以有以下幾種解決方案。
1 將main函數改爲Winmain函數
2 將/subsystem:windows開關改爲/subsytem:console
3全部刪除/subsytem:windows開關,讓連接程序根據源代碼中實現的函數進行正確的操作。
更改/subsystem:windows的方法:
右鍵點擊-->項目,選擇---->屬性,屬性頁 窗口彈出;
接着,點擊連接器->系統,在右側會看到“子系統”選項,在這裏可以選擇相應的連接程序開關.



問題五安裝安裝VAssistX
     下載地址http://yunpan.cn/ccZQpeN5i2wT3  訪問密碼 059c 
     解壓後直接安裝
去掉VS中對漢字的拼寫檢查後留下的紅色波浪線
原因在於VAssistx
在VAssistX菜單欄->Visual Assist X Options->展開Advanced->Underlines->把underlines spelling errors in comments and strings using的鉤去掉就行了

Window下配置OpenGL ES開發環境

1. 下載AMD的OpenGL ES2.0的模擬器,下載地址:
http://www.opengles-book.com/ESEmulator.2009-04-28-v1.4.APRIL_2009_RELEASE.msi

2. 下載《OpenGL ES2.0 Programming guide》裏的例子代碼,下載地址:
http://www.opengles-book.com/OpenGL_ES_Programming_Guide_v1.0.2.zip

3. 安裝vs, 我機器上安裝的是vs2012。

4. 安裝OpenGL ES2.0模擬器, 將安裝目錄下的AMD\OpenGL ES 2.0 Emulator v1.4\bin 以及AMD\OpenGL ES 2.0 Emulator v1.4\lib 裏的 libEGL.dll
、 libGLESv2.dll 和 libEGL.lib 、libGLESv2.lib分別拷貝至vs2012\VC的bin和lib文件夾下。

5. 解壓上面下載的例子代碼

6. 打開OpenGL_ES_Programming_Guide_v1.0.2\Chapter_2\Hello_Triangle下的sln文件

7. 如果生成解決方案的時候出現了類似於does not match the Linker’s OutputFile property value…的警告信息,程序運行會提示沒有找到exe文件,
解決方案:
(1) 右鍵項目esUtil,配置屬性-庫管理器-常規-輸出文件,把$(OutDir)esUtil_d.lib改成$(OutDir)esUtil.lib
(2) 右鍵項目Hello_Triangle:
配置屬性-鏈接器-常規,把輸出文件改成:Debug\$(ProjectName).exe
輸入-附加依賴項:esUtil_d.lib改成esUtil.lib

重新生成解決方案,運行效果如下:




Linux配置OpenGL ES開發環境
 

 0) sudo apt-get install libgles2-mesa-dev
 1) sudo apt-get install libgles1-mesa libgles1-mesa-dev
 2) sudo apt-get install freeglut3 freeglut3-dev
 3) gcc ./*.c -o "XXX" -L/usr/lib/x86_64-linux-gnu/ -lGLESv1_CM -lglut



編寫C/C++代碼問題

如果是windows程序:

1.菜單中選擇 Project->Properties, 彈出Property Pages窗口
2.在左邊欄中依次選擇:Configuration Properties->C/C++->Preprocessor,然後在右邊欄的Preprocessor Definitions對應的項中刪除_CONSOLE,
添加_WINDOWS.
3.在左邊欄中依次選擇:Configuration Properties->Linker->System,然後在右邊欄的SubSystem對應的項改爲Windows(/SUBSYSTEM:WINDOWS)

如果是控制檯程序:

1.菜單中選擇 Project->Properties, 彈出Property Pages窗口
2.在左邊欄中依次選擇:Configuration Properties->C/C++->Preprocessor,然後在右邊欄的Preprocessor Definitions對應的項中刪除_WINDOWS,
添加_CONSOLE.
3.在左邊欄中依次選擇:Configuration Properties->Linker->System,然後在右邊欄的SubSystem對應的項改爲CONSOLE(/SUBSYSTEM:CONSOLE)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章