Access violation. when using GLEW and GLFW - c++

I am sure that everything is linked correcly. I initially was using glload and glfw from the Unofficial GLSDK but then I decided to do away with glload which meant that I had to use glew in order to get at the modern headers.
#include <GL/glew.h>
#include <GL/glfw.h>
I have included glew before glfw as per the instructions.
During run time the OpenGL window opens
//(relevant code)
if(!glewInit()) {return -1; }
if(!glfwInit()) {return -1; }
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// also tried glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
if(!glfwOpenWindow(1024, 768, 8, 8, 8, 8, 24, 8, GLFW_WINDOW)){
glfwTerminate();
return -1;
}
glfwSetWindowTitle("OpenGL 3.2");
//init method
glGenVertexArrays(1, &vao); //<< Access violation here.
Any ideas what my problem is here?
I have looked at. "Access violation using VBO with glew" But it was no help.

glewInit is to be called after a OpenGL context has been created and bound to the current thread, i.e. after glfwOpenWindow in your case.

A little late, but figured I'd pipe in anyways.
As mentioned by datenwolf, in your relevant code posted the glewInit() should return an error due to it's positioning.
The other potential issue you could be encountering is described on http://www.opengl.org/wiki/OpenGL_Loading_Library under the GLEW section.
copy-paste from above:
GLEW has a problem with core contexts.
It calls glGetString(GL_EXTENSIONS)​,
which causes GL_INVALID_ENUM​ on GL 3.2+ core context as soon as glewInit()​ is called.
Solution for GLEW (also provided by above link) is to enable 'EXPERIMENTAL' support. Ex:
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if( err != GLEW_OK )
{
printf("GlewInit error");
exit(1);
}

Also late but for anyone still looking
glewExperimental = GL_TRUE;
Before initializing the context was got rid of the access violation error, but instead made the program exit with a GL_INVALID_ENUM error. With GLFW, I had to additionally comment out the window hints:
//glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
//glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
//glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
The programs then started compiling!

Related

Failed to create GLFW window?

I tried to create a window with the simplest code possible:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h> // Always include it before glfw.h
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
int main(void) {
glfwInit();
glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
glfwWindowHint(GLFW_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // Mac
GLFWwindow* window = glfwCreateWindow(720, 480, "OpenGL", NULL, NULL); // Create a window
if (window == NULL) {
fprintf(stderr, "Failed to create window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
return 0;
}
This is my Makefile:
game:
g++ src/main.cpp -std=c++17 -o play -I include -L lib -l glfw.3 -l GLEW.2.2
When I compile my code there is no error, but when I try to play my code I have this error:
Failed to create window
Invalid window hint 0x00000003
Invalid window hint 0x00000003
Context profiles are only defined for OpenGL version 3.2 and above
Can someone help me? I don't know why my window doesn't want to be created...
GLFW_VERSION_MAJOR & GLFW_VERSION_MINOR are not valid arguments to glfwWindowHint:
glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
^^^^^^^^^^^^^^^^^^ nope
glfwWindowHint(GLFW_VERSION_MINOR, 3);
^^^^^^^^^^^^^^^^^^ also nope
Use GLFW_CONTEXT_VERSION_MAJOR & GLFW_CONTEXT_VERSION_MINOR instead.
As per the docs, emphasis mine:
GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR specify the client API version that the created context must be compatible with. The exact behavior of these hints depend on the requested client API.
Note: Do not confuse these hints with GLFW_VERSION_MAJOR and GLFW_VERSION_MINOR, which provide the API version of the GLFW header.

GLFW: profile requested but WGL_ARB_create_context_profile is unavailable

I can't open an OpenGL window, due to following error message (I'm on Windows):
GLFW Error Code 65543: WGL: OpenGL profile requested but WGL_ARB_create_context_profile is unavailable.
It is likely that my problem is a driver problem. I tried to update them (using Intel Driver Update Utility), but it didn't do the trick (and my driver seemed to be already up to date). I use built-in Intel HD Graphics 3000. I also installed a OpenGL viewer, which tells me that my OpenGL version is 3.1).
Also, I tried this solution.
The whole C++ code is quite huge so I won't copy it all , but here is the interesting part:
if( !glfwInit() )
{
std::cerr<<"Failed to initialize GLFW\n"<<std::endl;
return -1;
}
glfwSetErrorCallback(glfwErrorCallback);
// Create the OpenGL window
glfwWindowHint(GLFW_DEPTH_BITS, 16);
glfwWindowHint(GLFW_SAMPLES, 4);
//Those stop GLFW from initializing successfully?
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);
// Open OpenGL fullscreen window
gGLFWWindow = glfwCreateWindow(gWidth,gHeight,"GLFW OpenGL Window",nullptr,nullptr);
if(!gGLFWWindow)
{
std::cerr<<"Failed to open GLFW window\n"<<std::endl;
glfwTerminate();
return -1;
}
// Disable VSync (we want to get as high FPS as possible!)
glfwMakeContextCurrent(gGLFWWindow);
glfwSwapInterval( 1 );
// Setting this is necessary for core profile (tested with MSVC 2013 x64, Windows 7)
glewExperimental = GL_TRUE;
// GLEW wraps all OpenGL functions and extensions
GLenum err = glewInit();
if(err != GLEW_OK)
{
std::cerr<<"Failed to initialize GLEW"<<std::endl;
std::cerr<<(char*)glewGetErrorString(err)<<std::endl;
glfwTerminate();
return -1;
}
glGetError(); //GLEW might cause an 'invalid enum' error, safely ignore it?
// Print OpenGL context information to console
ogl::printContextInformation();
// Perform our initialization (OpenGL states, shader, camera, geometry)
if(!init())
return -1;
It fails at this line :
gGLFWWindow = glfwCreateWindow(gWidth,gHeight,"GLFW OpenGL Window",nullptr,nullptr);
Does anyone have an idea of what I could do to solve this issue?
The answer is : I was requesting a Core 3.3 context whereas my version was OpenGL 3.1.
Deleting/commenting those lines will do the trick :
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);
I to had the same problem because my last machine has an openGl of version 3.3 and now because the one am using now has but an openGl 3.1 made me almost got frustrated
But De-comenting the "window hints", it solves the problem

Linker error in Dev C++: undefined reference to 'glfwInit' [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm trying to follow this tutorial for OpenGL. I originally copied the code by hand, but that wasn't working, so I've copy-pasted the code straight from the website. I keep getting this error:
[Linker error] undefined reference to 'glfwInit'
from this code (which feels longer than necessary):
//C++ standard headers
#include <stdio.h>
#include <stdlib.h>
//GLEW header
#include <GL/glew.h>
//GLFW header
#include <GL/glfw3.h>
int main()
{
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
if( window == NULL )
{
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do{
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
}
// Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
}
I've got no idea why it's not compiling. Anyone know what's going on?
EDIT: I'm using Dev-C++, as stated in the title.
undefined reference to 'glfwInit'
means that the linker did not find the library where glfwInit() is defined. You have to add glfw3.a to your linker input. Indeed, Dev-C++ use MinGW so unlike Visual Studio, the libraries can not be .lib.
To do that with Dev-C++, go to your 'project options', 'parameters', and 'add a library'. Then browse the explorer to find the glfw3.a I mentioned (usually in GLFW-<version>/lib/).

OpenGL with SDL2 doesn't return the right version

I have an SDL2 application in which I want to create an OpenGL 3.2 context. I googled a bit and started following this tutorial: http://open.gl/context#SDL
Everything seems to work except for the last step. When I had to implement this piece of code:
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
printf("%u\n", vertexBuffer);
My application doesn't seem to have a reference to the functor that should be there. I know there are some people here who had the same problem but I didn't find a solution there. When I output the GL_VERSION it says it's 1.1.0 although I say it should be 3.2.0. Here's my code:
// START SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
logSDLError(std::cout, "SDL_Init");
return 1;
}
// SETUP OPENGL SETTINGS
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
// OPENING WINDOW
m_pWindow = SDL_CreateWindow("SDL/OpenGL Game Client - With Networking Library", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
if (m_pWindow == nullptr)
{
logSDLError(std::cout, "CreateWindow");
return 2;
}
// CREATE AN OPENGL CONTEXT ASSOCIATED WITH THE WINDOW.
m_GlContext = SDL_GL_CreateContext(m_pWindow);
if( m_GlContext == NULL )
{
printf( "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError() );
}
//Initialize GLEW
glewExperimental = GL_TRUE;
GLenum glewError = glewInit();
if( glewError != GLEW_OK )
{
printf( "Error initializing GLEW! %s\n", glewGetErrorString( glewError ) );
}
printf((char*)glGetString(GL_VERSION));
I have a FirePro graphics card that should be able to run OpenGL 4.0. I checked my driver updates and everything should be fine + I get no compile warnings saying that something might be wrong with OpenGL or Glew or SDL.
One thing I had to do to make glGetString() working was to include GL\freeglut.h. I don't really know why that is because it doesn't say so in the tutorial I followed.
I found the problem. I forgot to mention that I'm using MSTSC to remote from my desktop to my laptop (which is easier to work) and apparently this changes the graphics device.
I tried opening my application from the actual laptop and suddenly it worked, giving the right OpenGL version and everything.
Stupid mistake, I should've found this earlier.

Invalid GL context with GLFW and Unofficial OpenGL SDK/GLEW

I have started work on a new project with OpenGL 3.3. I was using GLFW and GLEW for window setup and loading of GL functions, but switched to the Unofficial OpenGL SDK instead of GLEW. The problem remained, though:
I was getting a segmentation fault when calling glCreateShader(G_VERTEX_SHADER), and it turned out, that the function pointer was NULL. I later found out that it was caused by an invalid GL Context.
This is the setup code:
#include <glload/gl_3_3.h>
#include <glload/gll.h>
#include <GL/glfw.h>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include "Cube.h"
template <class T>
int arraySize(T *a) {
return (sizeof(a) / sizeof(*a));
}
int main() {
if(!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
// Open a window and create its OpenGL context
if(!glfwOpenWindow(1024, 768, 0,0,0,0, 0,0, GLFW_WINDOW)) {
fprintf(stderr, "Failed to open GLFW window\n");
glfwTerminate();
return -1;
}
if(LoadFunctions() == LS_LOAD_FAILED) {
fprintf(stderr, "Failed to load GL functions.\n");
return -1;
}
I have searched for answers on Google, and on here, but haven't been able to find anything. I also asked in the OpenGL IRC channel on Freenode, and they told me to try the Unofficial SDK instead of GLEW, because GLEW with the core profile is bad. This didn't work, though.
The most weird thing is, that it worked previously, with the exact same setup as now.
By the way, I am using Windows 7 x64 with the newest available drivers.
SOLUTION:
I was being dumb, and calling glCreateShader() before glewInit(). Sorry for being dumb :(