You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用gl.h/glu.h的OpenGL编译报错:undefined reference to 'main'

解决Linux下OpenGL编译“undefined reference to 'main'”错误
  • 错误核心原因:C++程序必须以main()函数作为执行入口,你的代码文件learnopengl2triangleglew.cpp中未定义该函数,链接器找不到程序启动点,因此抛出此错误。
  • 具体解决步骤:
    1. 添加main函数:在learnopengl2triangleglew.cpp或单独的代码文件中补充main函数,示例如下:
      #include "CGfxOpenGL.h"
      int main(int argc, char** argv) {
          CGfxOpenGL gfxInstance;
          // 初始化OpenGL环境与类实例
          gfxInstance.Init();
          // 启动渲染循环(需根据CGfxOpenGL类的逻辑实现循环逻辑,比如处理窗口事件、调用渲染方法)
          while (true) {
              gfxInstance.Render();
              // 此处可添加事件处理、窗口退出判断逻辑
          }
          return 0;
      }
      
    2. 确保编译所有相关文件:如果main函数写在单独文件(如main.cpp)中,编译命令需包含所有cpp源文件:
      g++ -o test learnopengl2triangleglew.cpp main.cpp -lGL -lGLU -ldl
      
    3. 补充窗口初始化逻辑(若需):如果你的CGfxOpenGL类依赖窗口环境(如GLUT/GLFW),需在main函数中完成窗口创建与初始化,以GLUT为例:
      #include <GL/glut.h>
      #include "CGfxOpenGL.h"
      CGfxOpenGL gfxInstance;
      
      void renderFrame() {
          gfxInstance.Render();
          glutSwapBuffers();
      }
      
      int main(int argc, char** argv) {
          glutInit(&argc, argv);
          glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
          glutInitWindowSize(800, 600);
          glutCreateWindow("Rotating Object Demo");
          gfxInstance.Init();
          glutDisplayFunc(renderFrame);
          glutMainLoop();
          return 0;
      }
      
      此时编译命令需额外链接GLUT库:
      g++ -o test learnopengl2triangleglew.cpp main.cpp -lGL -lGLU -lglut -ldl
      

内容的提问来源于stack exchange,提问作者Freya the Goddess

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 08:27:36