GCC编译OpenGL报错(Windows11 x64系统下)

在Windows11平台下使用GNU GCC编译OpenGL时报错:
源代码如下:

#include <stdio.h>
#include <GL/glut.h>
#include <windows.h>
void myDisplay(void)
{
     glClear(GL_COLOR_BUFFER_BIT);
     glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
     glFlush();
}

int main(int argc, char *argv[])
{
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
     glutInitWindowPosition(100, 100);
     glutInitWindowSize(400, 400);
     glutCreateWindow("第一个OpenGL程序");
     glutDisplayFunc(&myDisplay);
     glutMainLoop();
     return 0;
}

在CMD命令行中运行报错如下:

F:\CPP>g++ GL.cpp -lglut32 -lopengl32 -o testGL.exe
D:/Program Files (x86)/GCC/x86_64-12.2.0-release-win32-seh-msvcrt-rt_v10-rev2/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lglut32: No such file or directory
collect2.exe: error: ld returned 1 exit status

怎么解决?

  • 这篇博客: 在Windows 10上安装GCC中的 2.1.3 配置环境变量 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    • 参考Windows 环境变量配置教程
    • 将上图中的bin添加到Windows 的环境变量path中