PDA

View Full Version : First Glew + GLFW program gives core dumped!



kamhagh
12-10-2014, 09:49 PM
Hi, this is killing me i just wanted to start learning OpenGL and now on my first program i get core dumped right after made window !

this is my code:

#include <iostream>
using namespace std;

// GLEW
#define GLEW_STATIC
#include <GL/glew.h>

// GLFW
#include <GLFW/glfw3.h>

// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);

// The MAIN function, from here we start our application and run our Program/Game loop
int main()
{
// Init GLFW
cout << "Starting to Init GLFW\n";
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);


cout << "Done initglfw\n";

GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr); // Windowed
glfwMakeContextCurrent(window);

cout << "Made Window\n";

// Set the required callback functions
glfwSetKeyCallback(window, key_callback);

cout << "seted the reqyured callbackfunctions\n";

// Initialize GLEW to setup the OpenGL Function pointers
glewExperimental = GL_TRUE;
glewInit();

cout << "did glewInit\n";

// Define the viewport dimensions
glViewport(0, 0, 800, 600);

cout << "Going in to the game loop\n";

// Program loop
while(!glfwWindowShouldClose(window))
{
cout <<"doing glwfollevents();\n";
// Check and call events
glfwPollEvents();

// Render
// Clear the colorbuffer

//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
//glClear(GL_COLOR_BUFFER_BIT);

// Swap the buffers
glfwSwapBuffers(window);
}

glfwTerminate();
return 0;
}

// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
cout << key << endl;
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}


i tried exmples included in glfw-3.0.4 and all worked fine, i don't know if they come precompiled but i ran cmake . and found them here !

please help me start opengl faster :) and using the same command without -lGLEW i compiled some example from glfw and it workeds, its a spining triangle!

Agent D
12-11-2014, 08:40 AM
Hi, this is killing me i just wanted to start learning OpenGL and now on my first program i get core dumped right after made window !


....
cout << "Made Window\n";

// Set the required callback functions
glfwSetKeyCallback(window, key_callback);

cout << "seted the reqyured callbackfunctions\n";
....


So your code crashes somewhere in those lines? So logically it must be the glfwSetKeyCallback call. I assume key_callback can't be NULL. Is window NULL? What if you comment
the key ballback out? Maybe the calling conventions don't match. Did you try using a debugger? valgrind?



i tried exmples included in glfw-3.0.4 and all worked fine, i don't know if they come precompiled but i ran cmake . and found them here !

Running cmake generates build system files (e.g. makefiles), it doesn't compile anything. Either you didn't mention building the things or you used precompiled versions.


please help me start opengl faster :) and using the same command without -lGLEW i compiled some example from glfw and it workeds, its a spining triangle!
You might not like my suggesion since it's not "the fastest way", but learn and understand the programming language you are using, understand what the compiler does,
what the linker does and what all those flags mean.

kamhagh
12-11-2014, 11:59 AM
So your code crashes somewhere in those lines? So logically it must be the glfwSetKeyCallback call. I assume key_callback can't be NULL. Is window NULL? What if you comment
the key ballback out? Maybe the calling conventions don't match. Did you try using a debugger? valgrind?


Running cmake generates build system files (e.g. makefiles), it doesn't compile anything. Either you didn't mention building the things or you used precompiled versions.


You might not like my suggestion since it's not "the fastest way", but learn and understand the programming language you are using, understand what the compiler does,
what the linker does and what all those flags mean.

thanks SO much for answering, and sorry for not including more info (i thought i already did! :|)

this is the gdb output:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004055eb in glfwSetKeyCallback ()
(gdb) bt
#0 0x00000000004055eb in glfwSetKeyCallback ()
#1 0x0000000000403943 in main ()
(gdb) Quit

the last output is made window, and when i remove the glfsetkeycallback the last thing it gives is the "Going in to the game loop" !

i know what compiler options does and other stuff ! but if theres anything i would gladly learn if you tell me! :)


this is how i compile it:

first i do this: g++ -c first.cpp -std=c++11
second i do this: g++ first.o -o first.exec -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -lGLEW

and simply : ./first.exec

kamhagh
12-12-2014, 07:49 AM
window is 0/null :O why is it 0?!

new code, it just returns after window !

here's new code:


#include <iostream>
using namespace std;

// GLEW
#define GLEW_STATIC
#include <GL/glew.h>

// GLFW
#include <GLFW/glfw3.h>

// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);

// The MAIN function, from here we start our application and run our Program/Game loop
int main()
{
// Init GLFW
cout << "Starting to Init GLFW\n";
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);


cout << "Done initglfw\n";

GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr); // Windowed
if(window == NULL)
{
cout << "Window is null\n\n\n";
return -1;
}
cout << window;
glfwMakeContextCurrent(window);

cout << "Made Window\n";

// Set the required callback functions
//glfwSetKeyCallback(window, key_callback);

cout << "Done callbackfunctions\n";

// Initialize GLEW to setup the OpenGL Function pointers
//glewExperimental = GL_TRUE;
glewInit();

cout << "did glewInit\n";

// Define the viewport dimensions
glViewport(0, 0, 800, 600);

cout << "Going in to the game loop\n";

// Program loop
while(!glfwWindowShouldClose(window))
{
cout <<"doing glwfollevents();\n";
// Check and call events
glfwPollEvents();

// Render
// Clear the colorbuffer

//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
//glClear(GL_COLOR_BUFFER_BIT);

// Swap the buffers
glfwSwapBuffers(window);
}

glfwTerminate();
return 0;
}

// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
cout << key << endl;
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}

Agent D
12-12-2014, 05:24 PM
window is 0/null :O why is it 0?!

new code, it just returns after window !

Apparently creating the GLFW window fails for some reason? Does your OpenGL(R) implementation actually offer
the GL version you requested (3.3 core)? Try running the following command:
$ glxinfo | grep -i "OpenGL version"

On a side note, I once had a problem with GLFW on Windows(R) a few years ago. When linking statically,
glfwOpenWindow failed. As it turned out during debugging, it failed to load some libraries at runtime. For
whatever obscure reason, that didn't occour when linking against GLFW dynamically.





....
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr); // Windowed
if(window == NULL)
{
cout << "Window is null\n\n\n";
return -1;
}
....


I can see some C++11 magic there. What value does "nullptr" have when casting it to a pointer? Why can't we simply use "NULL" here? Does it change something?




this is the gdb output:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004055eb in glfwSetKeyCallback ()
(gdb) bt
#0 0x00000000004055eb in glfwSetKeyCallback ()
#1 0x0000000000403943 in main ()
(gdb) Quit

Can you try building GLFW debug binaries and stepping into glfwSetKeyCallback( ) to determine where it fails?


By the way, cmake is a build system generator. When running cmake, it generates e.g. a Makefile or a VisualStudio(R) solution. Running this:
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_BUILD_TYPE=Debug
$ make

should suffice to compile a debug build for most cmake based projects.

GClements
12-12-2014, 06:17 PM
I can see some C++11 magic there. What value does "nullptr" have when casting it to a pointer? Why can't we simply use "NULL" here? Does it change something?

nullptr can be implicitly cast to any pointer type, or to any pointer-to-member type.

NULL is a macro which expands to an integral constant expression that evaluates to zero (any integral type, not necessarily "int"). This is problematic for C++ because of overloading. If you have overloads for e.g. int, long, and void*, passing NULL might select either the int or long version; it won't select the void* version. So you have to explicitly cast it to the correct type; but that assumes that you know what the correct type is; this isn't always straightforward, particularly when templates are involved.

Provided that the prototype for glfwCreateWindow() is in scope, the two nullptr arguments will be implicitly cast to GLFWmonitor* and GLFWwindow* respectively.

According to the GLFW documentation (http://www.glfw.org/docs/latest/group__window.html#ga5c336fddf2cbb5b92f65f10fb6043 344), glfwCreateWindow() will return a null pointer if an error occurs. The only potential error which it explicitly documents is:

Windows: Window creation will fail if the Microsoft GDI software OpenGL implementation is the only one available.

But if the example programs work, that suggests a compilation issue rather than a problem with the OpenGL implementation.

My first suspicion would be something related to GLEW. Try compiling a test program which doesn't use GLEW but just creates a window. Then try including the GLEW header and linking against the GLEW library.

kamhagh
12-13-2014, 12:24 AM
Apparently creating the GLFW window fails for some reason? Does your OpenGL(R) implementation actually offer
the GL version you requested (3.3 core)? Try running the following command:
$ glxinfo | grep -i "OpenGL version"

On a side note, I once had a problem with GLFW on Windows(R) a few years ago. When linking statically,
glfwOpenWindow failed. As it turned out during debugging, it failed to load some libraries at runtime. For
whatever obscure reason, that didn't occour when linking against GLFW dynamically.


I can see some C++11 magic there. What value does "nullptr" have when casting it to a pointer? Why can't we simply use "NULL" here? Does it change something?



Can you try building GLFW debug binaries and stepping into glfwSetKeyCallback( ) to determine where it fails?


By the way, cmake is a build system generator. When running cmake, it generates e.g. a Makefile or a VisualStudio(R) solution. Running this:
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_BUILD_TYPE=Debug
$ make

should suffice to compile a debug build for most cmake based projects.

OpenGL version string: 3.0 Mesa 10.1.3
:O that most be the reason :| sorry im a total noob at opengl! (i also tought 3.3 is the old version and my computer has it :|) i googled around about how to get it, nothing came, can you tell me how?!

if it helps i have Gigabyte R7 250x 2GB OC ! it supports DX11 and opengl 4.2!

kamhagh
12-13-2014, 01:09 AM
nullptr can be implicitly cast to any pointer type, or to any pointer-to-member type.

NULL is a macro which expands to an integral constant expression that evaluates to zero (any integral type, not necessarily "int"). This is problematic for C++ because of overloading. If you have overloads for e.g. int, long, and void*, passing NULL might select either the int or long version; it won't select the void* version. So you have to explicitly cast it to the correct type; but that assumes that you know what the correct type is; this isn't always straightforward, particularly when templates are involved.

Provided that the prototype for glfwCreateWindow() is in scope, the two nullptr arguments will be implicitly cast to GLFWmonitor* and GLFWwindow* respectively.

According to the GLFW documentation (http://www.glfw.org/docs/latest/group__window.html#ga5c336fddf2cbb5b92f65f10fb6043 344), glfwCreateWindow() will return a null pointer if an error occurs. The only potential error which it explicitly documents is:


But if the example programs work, that suggests a compilation issue rather than a problem with the OpenGL implementation.

My first suspicion would be something related to GLEW. Try compiling a test program which doesn't use GLEW but just creates a window. Then try including the GLEW header and linking against the GLEW library.

i got this from the internet, it uses glew, but shows that my glew is fine !


// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
glBegin(GL_TRIANGLES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-0.6f, -0.4f, 0.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(0.6f, -0.4f, 0.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.6f, 0.f);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}

edit: i realized that after posting, it doesn't even have glewinit();, why than does it have glew included ?!! + on my code glfwSetKeyCallback(window, key_callback); is before glewinit(); i guess it has to be initialized before it makes different right?!
commenting setting version line doesn't help anything, still null !


Can you try building GLFW debug binaries and stepping into glfwSetKeyCallback( ) to determine where it fails?


By the way, cmake is a build system generator. When running cmake, it generates e.g. a Makefile or a VisualStudio(R) solution. Running this:
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_BUILD_TYPE=Debug
$ make

should suffice to compile a debug build for most cmake based projects.

sorry for doing many replies, but i don't understand what you mean by this part :| the tutorial i read was only about C++ itself ! (cplusplus.com)
just running the commands you gave me from glfw folder gives me this :
$ cmake .. -DCMAKE_BUILD_TYPE=Debug
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Using X11 for window creation
-- Using GLX for context creation
-- Configuring done
-- Generating done
-- Build files have been written to: /home/k/glfw-3.0.4

kamhagh
12-14-2014, 01:21 AM
i just needed an restart after updating drivers ! stupid :D this is the best day ever ! Thanks a lot!



while(this.isbeating())
thanks(all);


:)