我正在学习OpenGL并面临这个问题:
源代码:
#include <iostream>
#include <glad/glad.h>
int nrAttributes;
int main()
{
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
std::cout << "max vertex: " << nrAttributes << std::endl;
return 0;
}
我用这个命令编译我的代码:
g++ main.cpp --std=c++11 -o main.o src/glad.c -I include -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl
我在linux mint 18.04 x64
GL信息:glxinfo | grep OpenGL
OpenGL vendor string: X.Org
OpenGL renderer string: AMD TURKS (DRM 2.50.0 / 4.13.0-38-generic, LLVM 5.0.0)
OpenGL core profile version string: 3.3 (Core Profile) Mesa 17.2.8
OpenGL core profile shading language version string: 3.30
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 17.2.8
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.0 Mesa 17.2.8
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.00
OpenGL ES profile extensions:
当我运行代码时,我得到以下输出:
Segmentation fault (core dumped)
任何人都可以解释为什么会这样吗?
答案 0 :(得分:1)
正如@ Rabbid76所指出的,我需要一个有效的上下文来调用opengl函数。
这是工作代码,希望它能帮助其他人。
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main()
{
glfwInit();
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
int nrAttributes;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
std::cout << "Maximum nr of vertex attributes supported: " << nrAttributes << std::endl;
return 0;
}