Question

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?

Was it helpful?

Solution

"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

OTHER TIPS

You should add link X11 library setting -lX11 to your project. If you are using Eclipse navigate to projectproperties->C/C++ Build->Settings->Tool Settings->GCC Linker->Libraries and add "X11"

Add header-

#include<X11/Xlib.h>

Compile your source code using-

gcc <filename.extension> -lX11

Tested in Ubuntu 16.04 LTS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top