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

GCC编译OpenGL项目报错true未定义及GLFW相关标识符未声明问题

错误修复方案
  • true未定义错误修复

你使用GCC编译.c后缀文件时默认按C语法处理,C语言没有内置true关键字,两种修复方式可选:

  1. 直接将代码中两处true替换为1,C语言中非0值代表布尔真
  2. 在头文件引入区域添加 #include <stdbool.h>,该头文件封装了true/false宏定义
  • GLFW_CONTENT_VERSION_MINOR未定义错误修复

属于拼写错误,替换为正确的宏名 GLFW_CONTEXT_VERSION_MINOR 即可

  • glfwMakeContentCurrent隐式声明警告修复

属于拼写错误,替换为正确的函数名 glfwMakeContextCurrent 即可

修复后完整代码

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <GL/glew.h>
#include <GL/glfw3.h>

int main(){
   glewExperimental = true;
   if(!glfwInit()){
    printf("failed to initialize GLFW\n");
    return -1;
   }
   glfwWindowHint(GLFW_SAMPLES, 4);
   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

   GLFWwindow* window;
   window = glfwCreateWindow(1024, 768, "Game", NULL, NULL);
   if(window == NULL){
    printf("Sorry to say this, but your OpenGL version must be 3.3 or above! Thanks for playing, but to continue you must update your video card drivers or if you use an old GPU you may have to replace it with a new one to play this game. I will be developing my game for OpenGL 1 and 2 soon so stay on touch.");
    glfwTerminate();
    return -1;
   }
   glfwMakeContextCurrent(window);
   glewExperimental = true;
   if(glewInit() != GLEW_OK){
    printf("Failed to initilize GLEW");
    return -1;
   }
   return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 19:06:02