编译问题:error C2664
在《OpenGL Tutorial: (1) Setting up OpenGL with Visual Studio》一文中,我们为创建一个OpenGL工程做好了准备。接下来我们用这段程序来测试:
#include "stdafx.h"
#include <GL\glew.h>
#include <GL\freeglut.h>
static void keyboard (unsigned char key, int x, int y);
static void display (void);
int _tmain (int argc, _TCHAR* argv[])
{
glutInit (&argc, argv);
glutCreateWindow ("GLUT Test");
glutKeyboardFunc (&keyboard);
glutDisplayFunc (&display);
glutMainLoop ();
return 0;
}
static void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
exit (EXIT_SUCCESS);
break;
}
}
static void display ()
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0f, 0.0f, 0.0f);
glBegin (GL_POLYGON);
glVertex2f (-0.5f, -0.5f);
glVertex2f (0.5f, -0.5f);
glVertex2f (0.5f, 0.5f);
glVertex2f (-0.5f, 0.5f);
glEnd ();
glFlush ();
}
不幸的是编译的时候我们遇到了以下编译错误:
error C2664: 'void glutInit_ATEXIT_HACK(int *,char **)' : cannot convert argument 2 from '_TCHAR *[]' to 'char **'
绕开这个问题
显而易见,问题的原因是,glutInit() 这个函数的参数只支持ANSI的’char *’参数,而我们的Win32 Console Application的 int _tmain (int argc, _TCHAR argv[]) 函数传入了一个 ‘_TCHAR *[]’参数。
让我们暂时忘掉程序的国际化问题。只要把 int _tmain (int argc, _TCHAR* argv[]) 这个入口函数改成 int main (int argc, char* argv[]) 即可绕过此问题。
在使用MBCS字符集的情况下解决此问题
现在让我们正面面对此问题,着手解决。
可以在使用MBCS字符集的情况下解决此问题。方法是:
第1步:使用MBCS字符集
更改项目属性 General | Character Set 属性,将其设置为:
Use Multi-Byte Character Set
Screenshot:
第2步:在 stdafx.h 中增加宏定义
在 stdafx.h 中,增加一下宏定义:
#ifdef _MSC_VER // Check if MS Visual C compiler
# ifndef _MBCS
# define _MBCS // Uses Multi-byte character set
# endif
# ifdef _UNICODE // Not using Unicode character set
# undef _UNICODE
# endif
# ifdef UNICODE
# undef UNICODE
# endif
#endif
现在再编译,就能通过了。
在使用Unicode字符集的情况下解决此问题
但是MBCS字符集毕竟是过时(obsolete)的,现代C/C++程序在需要支持国际化特性是,大多采用Unicode字符集。所以下面给出使用Unicode字符集的情况下的解决方案。
第1步:使用Unicode字符集
更改项目属性 General | Character Set 属性,将其设置为:
Use Unicode Character Set
Screenshot:
第2步:在 stdafx.h 中增加宏定义
在 stdafx.h 中,增加一下宏定义:
#ifdef _MSC_VER // Check if MS Visual C compiler
# ifndef _MBCS
# define _MBCS // Uses Multi-byte character set
# endif
# ifdef _UNICODE // Not using Unicode character set
# undef _UNICODE
# endif
# ifdef UNICODE
# undef UNICODE
# endif
#endif
解决编译警告:warning C4133
此时编译倒是能通过,但是会有一个警告:
warning C4133: 'function' : incompatible types - from '_TCHAR **' to 'char **'
解决此警告的方式:
(待续)