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.