Cannot call XInitThreads

user3554800 picture user3554800 · May 10, 2014 · Viewed 11k times · Source

I have written an SFML C++ game, and tried to start using threads, but after a while everything crashes. After searching I found out the fix seems to be to call XInitThreads(); but this does not work somehow.

simplified code:

    #include <X11/Xlib.h>   

    int main() {        
    XInitThreads();
    //other stuff
    return 1337;
    }

The error message I get when i try to compile is "undefined reference to symbol 'XInitThreads'. Could it be that the header file is working but there is no file where that method is implemented?

Answer

Lie Ryan picture Lie Ryan · May 10, 2014

"undefined reference to symbol" is a linker error, not a compiler error. If you get this message, the compiler has already finished compiled the file into an object file, but is unable to find the shared library which contains the function to link the object file into an executable.

If you're using gcc, it generally means you have to add some -l flags, like so:

$ gcc prog.c -lX11

note that the order of -lX11 in the compiler argument matters, you would get an error if you do this:

$ gcc -lX11 prog.c
/tmp/ccBCxiFT.o: In function `main':
:(.text+0x5): undefined reference to `XInitThreads'
collect2: error: ld returned 1 exit status