在vs2008环境下写了一个OpenGL分片渲染的小程序,编译程序的时候,出现了好多既熟悉又陌生的错误,原先经常遇到,也在网上找到了相应的解决办法,但一直没有记录。于是乎经常出错,经常像无头苍蝇一样重新找答案。废话少说先描述错误。build工程时gl.h出现了一百多个语法错误,想都不用想应该不是OpenGL的问题,提示如下:
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2054: expected ‘(‘ to follow ‘WINGDIAPI‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2085: ‘APIENTRY‘ : not in formal parameter list
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2146: syntax error : missing ‘,‘ before identifier ‘glAccum‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2143: syntax error : missing ‘;‘ before ‘(‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2059: syntax error : ‘)‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1153) : error C2054: expected ‘(‘ to follow ‘WINGDIAPI‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1153) : error C2085: ‘APIENTRY‘ : not in formal parameter list
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1153) : error C2146: syntax error : missing ‘,‘ before identifier ‘glAlphaFunc‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1153) : error C2143: syntax error : missing ‘;‘ before ‘(‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1153) : error C2059: syntax error : ‘)‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1154) : error C2054: expected ‘(‘ to follow ‘WINGDIAPI‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1154) : error C2085: ‘APIENTRY‘ : not in formal parameter list
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1154) : error C2146: syntax error : missing ‘,‘ before identifier ‘glAreTexturesResident‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1154) : error C2143: syntax error : missing ‘;‘ before ‘(‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1154) : error C2059: syntax error : ‘)‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1155) : error C2054: expected ‘(‘ to follow ‘WINGDIAPI‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1155) : error C2085: ‘APIENTRY‘ : not in formal parameter list
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1155) : error C2146: syntax error : missing ‘,‘ before identifier ‘glArrayElement‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1155) : error C2143: syntax error : missing ‘;‘ before ‘(‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1155) : error C2059: syntax error : ‘)‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1156) : error C2054: expected ‘(‘ to follow ‘WINGDIAPI‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1156) : error C2085: ‘APIENTRY‘ : not in formal parameter list
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1156) : error C2146: syntax error : missing ‘,‘ before identifier ‘glBegin‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1156) : error C2143: syntax error : missing ‘;‘ before ‘(‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1156) : error C2059: syntax error : ‘)‘
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1157) : error C2054: expected ‘(‘ to follow ‘WINGDIAPI‘
......
初步断定应该是头文件包含顺序错误。首先在程序最开始的部分加上#<Windows.h>。
重新编译仍然出错。
然后将OpenGL库文件的包含顺序由
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
变成:
#incude <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
重新编译,不在出现上述错误,但引入了新的错误:
1>Linking...
1>TileRendering.obj : error LNK2019: unresolved external symbol __imp____glutInitWithExit@12 referenced in function _glutInit_ATEXIT_HACK@8
1>TileRendering.obj : error LNK2019: unresolved external symbol __imp____glutCreateWindowWithExit@8 referenced in function _glutCreateWindow_ATEXIT_HACK@4
1>E:\Test\TileRendering\Debug\TileRendering.exe : fatal error LNK1120: 2 unresolved externals
出现这种问题一般是函数没有暴露给外部或者lib文件没有添加,我将这些OpenGL32.lib glut32.lib Glaux.lib GLU32.lib文件加入到工程属性中还是出现这种问题。网上说的解决办法不外乎就是添加lib文件或将lib和dll文件放在工程目录下,这些办法都无法解决此问题。在一个不起眼的角落里找到了个与众不同的说法:
在glut.h中添加: #define GLUT_DISABLE_ATEXIT_HACK
抱着死马当活马医的态度,试了试,问题得到成功解决。
后记:最后那个错误,下一个新版本的OpenGL,可能就不会出现这个情况 ,具体没有试过。
原文:http://blog.csdn.net/hitheu/article/details/19075965