c++ - 在 Visual Studio 2010 中使用 OpenGL - 错误 LNK2019 : unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

标签 c++ visual-studio-2010 opengl

我正在 Visual Studio 中编译这个 OpenGL 程序。在阅读了大量文章后,我已经正确设置了它。我已将正确的库添加到链接器的附加依赖项中。但是我收到此错误:

错误 LNK2019:未解析的外部符号 WinMain@16 在函数 __tmainCRTStartup 中引用

我正在编译的代码是:

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

void init(void) 
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);
}

void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT);
   glColor3f (1.0, 1.0, 1.0);
   glLoadIdentity ();             /* clear the matrix */
           /* viewing transformation  */
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   glScalef (1.0, 2.0, 1.0);      /* modeling transformation */ 
   glutWireCube (1.0);
   glFlush ();
}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
   glMatrixMode (GL_MODELVIEW);
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (500, 500); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);
   glutMainLoop();
   return 0;
}

最佳答案

Windows 中有两种可执行文件:

  1. 控制台
  2. 图形用户界面( window )

唯一的区别是控制台可执行文件会自动打开控制台窗口,而 C/C++ CRT 运行时关联标准的 stdout/stdin/stderr 句柄以转到此窗口。否则这两种可执行类型之间没有区别 - 都可以创建新窗口、绘制内容、使用 OpenGL 等...

在 Visual Studio 中,如果您创建控制台应用程序 - 那么它希望您的入口点被称为“main”。但对于 GUI 应用程序,它期望入口点函数被称为“WinMain”。因此,如果您不想在应用程序启动时看到控制台窗口,您有两种选择:

  1. 更改项目链接器设置以指示您正在构建 GUI 应用程序(项目属性 -> 链接器 -> 系统 -> 子系统 = Windows)。这将需要调用 WinMain 的入口点函数:http://msdn.microsoft.com/en-us/library/ff381406.aspx
  2. 向链接器表明即使你想使用 GUI 应用程序,但你希望你的入口点被称为“main”。您可以在项目属性 -> 链接器 -> 高级 -> 入口点 = mainCRTStartup 中执行此操作。不要放在那里主要。放在那里 mainCRTStartup - 它是特殊的 C/C++ CRT 函数,它初始化标准 C 库并自动调用你的主函数。这是有关此设置的文档:http://msdn.microsoft.com/en-us/library/f9t8842e.aspx

使用第二个选项意味着您可以使用 GLUT,将入口点称为“main”并且在启动时不打开控制台窗口。

关于c++ - 在 Visual Studio 2010 中使用 OpenGL - 错误 LNK2019 : unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10155969/

相关文章:

c++ - Opengl 中飘扬的旗帜效果 (C++)

C++ 重载运算符并返回相同的对象

c++ - 'LIBCMT' 与使用其他库 + 未解析的外部符号冲突

visual-studio-2010 - 添加指向与 GAC 不同版本的引用

c# - 如何断言字符串列表中的任何字符串是否包含在实际字符串中

c++ - OpenGL:如何只清除屏幕的一部分?

c++ - 跟踪自动变量的生命周期?

c++ - 为什么 std::getline() 在格式化提取后跳过输入?

visual-studio-2010 - 应用程序在 Visual Studio 中崩溃,但仅在设置断点之后

opengl - OpenGL是否会生成一个值为0的texture-\framebuffer-\shader-等ID?