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

GLUT无法在电脑运行:使用C++和GLUT创建立方体时遇未定义引用错误

Fixing GLUT Undefined Reference Errors in Your C++ Cube Project

Hey there, those undefined reference errors you’re hitting are classic linker issues—here’s what’s going on: your compiler knows what GLUT functions like glutInit or glutMainLoop are (since you probably included the GLUT header), but it can’t find the actual precompiled GLUT library code to connect your code to. Let’s fix this step by step.

First, Confirm Your Header Include

Double-check that you have the right GLUT header at the top of q-3d.cpp:

#include <GL/glut.h>

(On some systems this might be <glut.h> or <GLUT/glut.h>—adjust if needed based on where your headers are stored.)

Fix by Platform

Linux (GCC/G++)

When compiling, you need to tell the linker to link against the GLUT library (plus OpenGL dependencies). Use this command:

g++ q-3d.cpp -o q-3d -lglut -lGL -lGLU

If you get a "cannot find -lglut" error, install the GLUT dev package first. On Debian/Ubuntu-based distros:

sudo apt-get install freeglut3-dev

Windows (MinGW)

MinGW needs you to point to the GLUT library files. Use a command like this (replace /path/to/glut/lib with the actual folder where your GLUT libs are):

g++ q-3d.cpp -o q-3d -L/path/to/glut/lib -lglut32 -lglu32 -lopengl32

Make sure your GLUT header files are in MinGW’s include path too.

macOS

macOS has phased out GLUT, but you can still use it via Homebrew. First install freeglut:

brew install freeglut

Then compile with:

g++ q-3d.cpp -o q-3d -lglut -lGL -lGLU

If you prefer the legacy system framework, use this instead:

g++ q-3d.cpp -o q-3d -framework GLUT -framework OpenGL

Quick Troubleshooting Tip

Make sure you’re not mixing 32-bit and 64-bit libraries—if your compiler is 64-bit, use the 64-bit GLUT library, and vice versa. That’s a common gotcha!

内容的提问来源于stack exchange,提问作者John Wilson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:24:01