Why is simply GL binding causing segfault?

I am writing an OpenGL application on Linux and I can easily create a window using GLUT , but as soon as I even get in -lGL with -lGL , I get segfault. Any ideas that might be causing this?

I do not get any warnings or compiler errors even with -Wall . Only when I run the program does it give me segfault.

I have not used gdb before, but does this information help?

 (gdb) run Starting program: /home/drjrm3/code/dc/c++/dc.exe Program received signal SIGSEGV, Segmentation fault. 0x0000000000000000 in ?? () (gdb) backtrace #0 0x0000000000000000 in ?? () #1 0x00007ffff32e8291 in ?? () from /lib/x86_64-linux-gnu/libdl.so.2 #2 0x00007ffff32e86d7 in ?? () from /lib/x86_64-linux-gnu/libdl.so.2 #3 0x00007ffff32e8198 in dlsym () from /lib/x86_64-linux-gnu/libdl.so.2 #4 0x00007ffff78ef6be in ?? () from /usr/lib/nvidia-340/libGL.so.1 #5 0x00007ffff78d3516 in ?? () from /usr/lib/nvidia-340/libGL.so.1 #6 0x00007ffff7dea0fd in ?? () from /lib64/ld-linux-x86-64.so.2 #7 0x00007ffff7dea223 in ?? () from /lib64/ld-linux-x86-64.so.2 #8 0x00007ffff7ddb30a in ?? () from /lib64/ld-linux-x86-64.so.2 #9 0x0000000000000001 in ?? () #10 0x00007fffffffc9f0 in ?? () #11 0x0000000000000000 in ?? () (gdb) 

I brought this to a minimal example, now I'm still confused about what is going on:

 #include <cstdio> #include <string> #include <cstdlib> #include <iostream> #include <vector> #include <GL/freeglut.h> #include <GL/gl.h> using namespace std; /********************\ * Global variables * \********************/ string fname; // gl vars int WinWidth = 800; int WinHeight = 800; int WinPos1 = 400; int WinPos2 = 400; void gl2Dinit(int argc, char** argv); void myInit(); void draw(); void mouseFunc(int button, int state, int x, int y); void keyboardFunc(unsigned char key, int x, int y); /****************\ * Main routine * \****************/ int main(int argc, char** argv) { fprintf(stderr, "We got this far ...\n"); return 0; } void gl2Dinit(int argc, char** argv) { // glutInit(&argc, argv); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); /// define window prorperties glutInitWindowSize(WinWidth, WinHeight); glutInitWindowPosition(WinPos1, WinPos2); glutCreateWindow("Title!"); /// intitialize myInit(); /// callback functions glutDisplayFunc(draw); glutMouseFunc(mouseFunc); glutKeyboardFunc(keyboardFunc); // glut main loop glutMainLoop(); } void myInit() { } void draw() { } void mouseFunc(int button, int state, int x, int y) { } void keyboardFunc(unsigned char key, int x, int y) { } 

I compile with g++ -g -o dc.exe xdriver.cpp -I/usr/include/GL -lglut -lGL -DLINUX -Wall , and when I run the code, it doesn't print anything ... I just get a Segmentation fault (core dumped) . What bothers me is that he should do nothing but print, and then leave ... but for some reason it disappears.

As soon as I remove -lGL , I run the program and it prints “We got this far ...” and then exit.

+6
source share
1 answer

After the same problem with the trivial OpenGL program, I found a solution here .

Thus, just adding -pthread to the compiler flags did not work for me because I did not use libpthread anywhere, so it was not actually connected. The trick was to make g ++ think that I am using libpthread and thus make it be connected by adding this small snippet to the main.cpp file:

 #if defined(__unix__) #include <pthread.h> void* simpleFunc(void*) { return NULL; } void forcePThreadLink() { pthread_t t1; pthread_create(&t1, NULL, &simpleFunc, NULL); } #endif 
0
source

All Articles