为什么赢了';这个OpenGL程序不能打开一个窗口吗? 我目前正在尝试学习C++和OpenGL,我从下面的代码示例开始: simple.cpp

为什么赢了';这个OpenGL程序不能打开一个窗口吗? 我目前正在尝试学习C++和OpenGL,我从下面的代码示例开始: simple.cpp,c++,opengl,glut,C++,Opengl,Glut,因此,我采用以下方式调整命令: gcc -Wno-deprecated-declarations -o simple simple.cpp -framework GLUT -framework OpenGL -framework Carbon 这似乎可行,并创建了一个名为simple的可执行文件 我被告知此代码应在黑色背景上生成一个白色正方形,如下所示: gcc -Wno-deprecated-declarations -o hello hello.c -framework GLUT -fra

因此,我采用以下方式调整命令:

gcc -Wno-deprecated-declarations -o simple simple.cpp -framework GLUT -framework OpenGL -framework Carbon
这似乎可行,并创建了一个名为
simple
的可执行文件

我被告知此代码应在黑色背景上生成一个白色正方形,如下所示:

gcc -Wno-deprecated-declarations -o hello hello.c -framework GLUT -framework OpenGL -framework Carbon 

但是,当我尝试使用
/simple
从终端运行此文件时,程序会连续运行,但什么也没有发生(即根本没有生成窗口),因此我必须从终端终止程序

我是否在这里做错了什么,或者这是MacOS上给定代码的预期结果


编辑1 为了了解会发生什么,我尝试使用以下代码:

你好,c 这很好。但是,当我尝试运行可执行文件时,出现以下错误:

GLUT Fatal API Usage: main loop entered with no windows created.
zsh: abort      ./hello
根据此错误消息,程序已终止,因为未创建任何窗口。但是,正如我所说,我的simple.cpp程序没有终止(我必须强制终止它),它是为创建窗口而编写的。所以我猜这意味着windows是在simple.cpp中创建的,但出于某种原因,它们不会出现在MacOS上?还有谁能证实这一点吗?问题是否在于MacOS上没有显示空白窗口,而您需要包含其他图形元素才能显示窗口


编辑2 问题是我的理解是,
glutCreateWindow(“simple”)simple.cpp
中的code>应该创建一个标题为“simple”的窗口,所以我不明白为什么它不工作


编辑3 多亏了德哈斯的回答,我才得以成功:

#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>

void init() {
        // code to be inserted here
}

void mydisplay() {
        glClear(GL_COLOR_BUFFER_BIT);
        // need to fill in this part
        // and add in shaders
}

int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutCreateWindow("simple");
        init();
        glutDisplayFunc(mydisplay);
        glutMainLoop();
}
#定义GL\u沉默\u弃用
#包括
void init(){
//要在此处插入的代码
}
void mydisplay(){
glClear(GLU颜色缓冲位);
//需要填写这部分吗
//和附加着色器
}
int main(int argc,字符**argv){
glutInit(&argc,argv);
创建窗口(“简单”);
init();
glutDisplayFunc(mydisplay);
glutMainLoop();
}

您没有在
simple.cpp
示例中包含对
glutInit()
的调用。在此初始化之前,您不能调用任何其他GLUT命令,这样做会导致未定义的行为。

不,它不应该绘制正方形。我不熟悉GLUT,但它最多只能打开一个黑色窗口。@HolyBlackCat,但我的却什么都不显示(没有生成窗口)。这个程序一直运行,没有任何东西,直到我从终端终止它。@HolyBlackCat看起来你确实是对的。谢谢你的回答。其中
simple.cpp
glutInit(&argc,argv)开始?它应该在
createwindow(“simple”)之前?^是!这样做是有效的(参见EDIT3)!!谢谢你的帮助!!我认为“在初始化之前,您不能调用任何其他GLUT命令”将非常清楚。
gcc -Wno-deprecated-declarations -o hello hello.c -framework GLUT -framework OpenGL -framework Carbon 
GLUT Fatal API Usage: main loop entered with no windows created.
zsh: abort      ./hello
#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>

void init() {
        // code to be inserted here
}

void mydisplay() {
        glClear(GL_COLOR_BUFFER_BIT);
        // need to fill in this part
        // and add in shaders
}

int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutCreateWindow("simple");
        init();
        glutDisplayFunc(mydisplay);
        glutMainLoop();
}