6

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

I don't get any compiler warnings or errors even with -Wall. Only when I run the program does is give me a segfault.

I have never 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 got it down to a minimum example now it 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 prints nothing out ... I just get Segmentation fault (core dumped). What confuses me is that it should be doing nothing but printing out and then quitting ... but somehow it segfaults.

As soon as I take off -lGL, I run the program and it prints out "We got this far ..." and then exits.

drjrm3
  • 4,474
  • 10
  • 53
  • 91
  • 2
    Run your program under gdb and indicate where it is segfaulting. – Jonathon Reinhart Sep 27 '15 at 04:32
  • 1
    What's the smallest source-code example that you can produce which runs without `-lGL` but segfaults with? Also include your build command-line. – Brent Bradburn Sep 27 '15 at 04:37
  • @nobar - I've been trying to rebuild a minimum example for the past hour to isolate the problem with no luck so far! My build command is `g++ -O2 -o dc.exe xdriver.cpp fio.o glfuns.o -I/usr/include/GL -lglut -lGL -DLINUX -Wall`. It's also worth nothing that this segfault happens **even when I don't call any GL or GLUT functions**. – drjrm3 Sep 27 '15 at 04:41
  • 1
    You probably want to at least include `-g` on the `g++` command-line in order to make the `backtrace` a bit more interesting. Also, you might try removing the `-O2`. – Brent Bradburn Sep 27 '15 at 04:46
  • Compiled with `-g` and took off `-O2` ... same output. – drjrm3 Sep 27 '15 at 05:06
  • 1
    It looks like either your linker's libdl.so or your libGL.so is just broken. – derhass Sep 27 '15 at 12:10
  • 1
    I had the same problem with nvidia drivers on Gentoo Linux. You may want to look at http://stackoverflow.com/questions/31579243/segmentation-fault-before-main-when-using-glut-and-stdstring . Adding -pthread to the command line fixed it for me. – qbt937 Nov 13 '15 at 01:36

1 Answers1

0

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

In summary, merely adding -pthread to the compiler flags didn't work for me because I wasn't using libpthread anywhere so it didn't actually get linked in. The trick was to force g++ into thinking I was using libpthread, and thus force it to be linked, by adding this little snippet to my 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
daiscog
  • 11,441
  • 6
  • 50
  • 62