FreeGLUT Tips: Resolve compile error C2664: cannot convert argument 2 from '_TCHAR *[]' to 'char **'

编译问题: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:
使用MBCS字符集

第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:
使用Unicode字符集

第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 **'

解决此警告的方式:

(待续)

参考文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值