我在Eclipse中有一个简单的OpenGL/GLFW测试程序
#include <iostream>
#include <string>
#include <GL/glew.h>
#define GLFW_INCLUDE_GLU
#include <GLFW/glfw3.h>
void errorCallback(int error, const char *description)
{
std::cerr << description << " (GLFW error " << error << ")" << std::endl;
}
int main(int argc, char **argv)
{
int returnValue = 0;
try {
// Initialise GLFW.
glfwSetErrorCallback(errorCallback);
if(!glfwInit()) throw std::string("Could not initialise GLFW");
/* ...do everything else... */
} catch(std::string const &str) {
std::cerr << "Error: " << str << std::endl;
returnValue = 1;
}
return returnValue
}但是,运行它会在控制台中显示以下内容:
X11: Failed to open X display (GLFW error 65542)
Error: Could not initialise GLFW也就是说,它在glfwInit()期间失败(我注释掉了所有的代码,以确保它不会在创建窗口或其他什么过程中实际发生)。但是,导航到构建目录(使用我的文件管理器,而不是Eclipse )并从那里手动启动非常有效。
有人知道问题出在哪里吗?
发布于 2013-08-18 23:34:37
Eclipse实际上并没有将任何环境变量传递给我的程序(谢谢datenwolf让我开始工作)。可以通过运行配置来选择传递给程序的环境变量,在"C/C++ Application“下选择适当的启动配置(我只有默认的一个),打开environment选项卡,然后按select按钮(它列出所有可用的环境变量)并选择您想要的环境变量。
发布于 2013-08-18 23:19:59
在我看来,Eclipse在启动程序时会清除所有或部分环境变量。环境变量DISPLAY告诉程序如何连接到X11服务器。如果没有这些信息,它就无法打开显示,从而导致错误。
验证这一点的简单测试:在glfwInit()之前添加以下内容(没关系,这不是C++,也不使用iostream,但对于快速测试来说,这是可以的:
fprintf(stderr, "DISPLAY=%s\n", getenv("DISPLAY"));您必须包括标题stdio.h和stdlib.h。
https://stackoverflow.com/questions/18304524
复制相似问题