最近一直在学习用c++编程,但是在使用GLEW和GLFW时遇到了问题。
来源(.cpp):
#include<iostream>
#define GLEW_STATIC
#include "GLEW/glew.h"
#include <GLFW/glfw3.h>
int main(){
std::cout<<"test";
GLFWWindow* window;
window = glfwCreateWindow(800,600,"Test", glfwGetPrimaryMonitor(),NULL);
return 0;
}
这个程序编译没有错误,但是当它运行时,尽管 std::cout 没有生成输出。然而,一旦 glfwCreateWindow 调用被注释掉,程序就会显示它应该的输出。
我对此感到非常困惑,想知道我是否可以做些什么来解决它。
编辑:我怀疑这是 std::cout 的问题,因为当我尝试像这样显示窗口时,程序仍然没有任何响应。
glfwShowWindow(window);
while(!glfwWindowShouldClose(window))glfwPollEvents();
glfwDestroyWindow(window);
编辑 2: 这是我现在的代码:
#define GLEW_STATIC
#include "GLEW/glew.h"
#include <GLFW/glfw3.h>
#include <iostream>
int main(){
std::cout<<"test" << std::endl;
glfwInit();
GLFWwindow* window;
window = glfwCreateWindow(800,600,"TEST", NULL, NULL);
glfwShowWindow(window);
while(!glfwWindowShouldClose(window))glfwPollEvents();
glfwDestroyWindow(window);
glfwTerminate();
std::cin.get();
}
编辑 3:标题
编辑 4:经过一些实验,我发现在源文件中提及 glfw 调用似乎会阻止程序运行。以下不产生输出:
int main(){
std::cout<<"test" << std::endl;
std::cout.flush();
std::cin.get();
}
void test(){
glfwInit();
GLFWwindow* window;
window = glfwCreateWindow(800,600,"TEST", NULL, NULL);
glfwShowWindow(window);
while(!glfwWindowShouldClose(window))glfwPollEvents();
glfwDestroyWindow(window);
glfwTerminate();
}
最佳答案
stdout
没有输出的一些原因:
- 如果您在 Windows 上运行,您的程序可能是在 Windows 子系统中编译的。然后,任何打印到
stdout
的内容都将转到 bitbucket。如果您使用的是 MinGW 工具链,这由-mwindows
链接器标志控制 - 如果程序崩溃,则可能不会写入缓冲输出。因此,在执行任何其他操作之前尝试刷新输出流。请注意,程序可能会无声地崩溃。
您可以询问一些事情以了解更多信息:
- 当您使用编译器附带的调试器单步执行代码时会发生什么?
- 由于
glfwCreateWindow
隐藏了输出,它可能会在该函数内崩溃。你传递了正确的参数吗? glfwCreateWindow
是否返回有效指针?
关于使用 GLFW 调用时出现 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24462281/