Skip to content

Instantly share code, notes, and snippets.

@luismrsilva
Last active April 21, 2018 04:03
Show Gist options
  • Save luismrsilva/efe2eb2909d0352690bb5bd384730757 to your computer and use it in GitHub Desktop.
Save luismrsilva/efe2eb2909d0352690bb5bd384730757 to your computer and use it in GitHub Desktop.
Fix for segmentation fault before main when using OpenGL functions AND std under Linux Ubuntu.
/*
Fix for segmentation fault before main
when Using OpenGL functions AND std
under Linux Ubuntu.
2016-10-25 luismrsilva
The problem is similar to this (solution is based on it too):
http://stackoverflow.com/questions/31579243/segmentation-fault-before-main-when-using-glut-and-stdstring
To see details use:
$ export LD_DEBUG=all
before running.
Fix:
Link with -pthread
If it does not work, also put the following code somewhere to make g++ assume pthread is used.
*/
// We check for __unix__ because this was only tested on Linux.
#if defined(__unix__)
#include <pthread.h>
void* simpleFunc(void*) {
return NULL;
}
void forcePThreadLink() {
pthread_t t1;
pthread_create(&t1, NULL, &simpleFunc, NULL);
}
#endif
@avnomad
Copy link

avnomad commented Apr 21, 2018

Hello! I run into the same problem, so thanks for the snippet!

I used pthread_getconcurrency instead to make it a little simpler, as in:

#if defined __unix__

#include <pthread.h>
void forcePThreadLink() {
    pthread_getconcurrency();
}

#endif

As far as I can tell, this is only needed with g++: -pthead is enough for clang++.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment